fix(terminal): keep both startup failures on TerminalBackendCleanupError's cause

Renderers such as errorChain and the activation report expand
AggregateError members but otherwise follow only cause, so a cause of
just the setup failure hid the cleanup failure that may leave
resources alive. The cause is now an AggregateError of both failures.
An upgrade guide records that the class extends TerminalError instead
of AggregateError.

INIT_REJECTED is documented as the code a consumer that drives a shell
uses when the shell rejects its setup input, and the persistent bash
tool README records it.

Refs #7869
This commit is contained in:
sjw1231 committed 2026-11-06 17:31:56 +08:00
1 parent 05270c6fcb
commit 3ce9462e7f
11 files changed
+82 -22

No files matched your search

@@ -0,0 +1,16 @@
# Bilingual-pair consistency record for guide.md (docs/i18n/README.md): per heading
# section, a hash of its English and Chinese blocks outside code blocks and generated regions.
# After editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write docs/upgrade-guide/v0.2.2-rc.1/terminal-backend-cleanup-error/guide.md
/:
en: 91597636f5a71894
zh: 02d2517952d86cfd
/terminalbackendcleanuperror-extends-terminalerror:
en: cb11df977825904b
zh: 080f157e78942a9d
/terminalbackendcleanuperror-extends-terminalerror/change:
en: bf3bd46faf910ff0
zh: fd47d6e58f467a1e
/terminalbackendcleanuperror-extends-terminalerror/migration:
en: 85fe7dd354cf8657
zh: e39dc4ce137aeb13
@@ -0,0 +1,20 @@
---
kind: upgrade-guide
description: "TerminalBackendCleanupError extends TerminalError instead of AggregateError and carries the CLEANUP_FAILED code."
---
# `TerminalBackendCleanupError` extends `TerminalError`
English | [中文](guide.zh.md)
## Change
`@deepseek-ai/dsh-terminal` exports `TerminalBackendCleanupError`, which a PTY backend throws when startup failed and cleaning its partial resources also failed. It previously extended `AggregateError`. It now extends `TerminalError`, so it is a `HarnessError` with code `CLEANUP_FAILED`, and a tool result that carries it records `error.name` `TerminalBackendCleanupError` and `error.code` `CLEANUP_FAILED`. The constructor, the `name`, `spawnError`, `cleanupError`, and the `errors` list in that order are unchanged; `errors` is typed `readonly unknown[]` instead of `any[]`. `cause` is a new `AggregateError` of the same two failures. `error instanceof AggregateError` is now `false`.
Backends and consumers that only construct the error, read `spawnError` or `cleanupError`, or test `instanceof TerminalBackendCleanupError` are unaffected.
## Migration
1. Replace `error instanceof AggregateError` checks meant for this error with `error instanceof TerminalBackendCleanupError`, or with `error.code === 'CLEANUP_FAILED'` on a `TerminalError`.
2. Where TypeScript code reads members of `error.errors` without narrowing, narrow each `unknown` element before use, or read `spawnError` and `cleanupError`.
3. Confirm with `pnpm exec tsc --noEmit` in the consuming project, and check that a startup cleanup failure still reaches the handling path.
@@ -0,0 +1,20 @@
---
kind: upgrade-guide
description: "TerminalBackendCleanupError 改为继承 TerminalError 而非 AggregateError,并带 CLEANUP_FAILED 错误码。"
---
# `TerminalBackendCleanupError` 改为继承 `TerminalError`
[English](guide.md) | 中文
## 变更
`@deepseek-ai/dsh-terminal` 导出的 `TerminalBackendCleanupError` 由 PTY 后端在启动失败且清理部分资源也失败时抛出。它原先继承 `AggregateError`,现在继承 `TerminalError`,因此是带 code `CLEANUP_FAILED` 的 `HarnessError`,携带它的工具结果会记录 `error.name` `TerminalBackendCleanupError` 和 `error.code` `CLEANUP_FAILED`。构造函数、`name`、`spawnError`、`cleanupError` 以及按原顺序排列的 `errors` 列表不变;`errors` 的类型由 `any[]` 改为 `readonly unknown[]`。`cause` 新增为同样两个失败组成的 `AggregateError`。`error instanceof AggregateError` 现在为 `false`。
只构造该错误、读取 `spawnError` 或 `cleanupError`、或检查 `instanceof TerminalBackendCleanupError` 的后端与消费方不受影响。
## 迁移
1. 把针对该错误的 `error instanceof AggregateError` 检查改为 `error instanceof TerminalBackendCleanupError`,或对 `TerminalError` 检查 `error.code === 'CLEANUP_FAILED'`。
2. TypeScript 代码未经收窄就读取 `error.errors` 元素成员时,先收窄每个 `unknown` 元素,或改为读取 `spawnError` 与 `cleanupError`。
3. 在消费项目中运行 `pnpm exec tsc --noEmit` 确认通过,并检查启动清理失败仍会进入原有的处理路径。
@@ -27,8 +27,8 @@
en: b398f124ea7b77b0
zh: 50855e601e6c1b88
/deepseek-ai-dsh-tool-bash-persistent/use-this-package/what-can-go-wrong:
en: 52e0e1489c6f8071
zh: 27251d61351ee72d
en: 080e71c8720eab46
zh: 2b0266e78c868d9c
/deepseek-ai-dsh-tool-bash-persistent/understand-the-implementation:
en: 8d94a7381bca3fa9
zh: f4bd2866fff6fa8d
@@ -58,7 +58,7 @@ Commands share one shell per agent, so state persists until an `exit`, a timeout
### What can go wrong
A call without an owning agent session fails with `bash requires an owning agent session` (`ToolContextUnavailableError`, `TOOL_CONTEXT_UNAVAILABLE`), a blank command fails with `ToolArgsError` (`INVALID_ARGS`), and a composition without a PTY backend activates the tool but fails its first call with `no PTY backend registered for "shell"`. An interactive foreground child (for example a REPL) returns early with partial output only where the backend proves its stdin wait; elsewhere the call runs to `timeoutMs`, which closes the uncertain shell and reports the reset. Cancellation also resets and discards the result, even when a complete status marker is already observable. After cleanup settles, ToolRuntime publishes `Error: tool call aborted` with code `ABORTED`; cancellation reasons are not rendered as command output. A queued cancelled call sends no command.
A call without an owning agent session fails with `bash requires an owning agent session` (`ToolContextUnavailableError`, `TOOL_CONTEXT_UNAVAILABLE`), a blank command fails with `ToolArgsError` (`INVALID_ARGS`), a shell that does not accept the echo and history setup fails with `persistent bash shell did not accept initialization` (`TerminalError`, `INIT_REJECTED`), and a composition without a PTY backend activates the tool but fails its first call with `no PTY backend registered for "shell"`. An interactive foreground child (for example a REPL) returns early with partial output only where the backend proves its stdin wait; elsewhere the call runs to `timeoutMs`, which closes the uncertain shell and reports the reset. Cancellation also resets and discards the result, even when a complete status marker is already observable. After cleanup settles, ToolRuntime publishes `Error: tool call aborted` with code `ABORTED`; cancellation reasons are not rendered as command output. A queued cancelled call sends no command.
-----
@@ -58,7 +58,7 @@ kind: "package-reference"
### 可能出什么问题
没有拥有者 agent 会话的调用会以 `bash requires an owning agent session`(`ToolContextUnavailableError`,`TOOL_CONTEXT_UNAVAILABLE`)失败,空白命令以 `ToolArgsError`(`INVALID_ARGS`)失败,没有 PTY 后端的组合会激活该工具,但首次调用以 `no PTY backend registered for "shell"` 失败。交互式前台子进程(例如 REPL)只有在后端证明其 stdin 等待时才提前返回部分输出;否则调用一直运行到 `timeoutMs`,随后关闭不确定的 shell 并报告重置。取消也会重置并丢弃结果,即使完整状态标记已经可观察。清理完成后,ToolRuntime 发布 `Error: tool call aborted`,错误码为 `ABORTED`;取消原因不作为命令输出渲染。排队期间被取消的调用不发送命令。
没有拥有者 agent 会话的调用会以 `bash requires an owning agent session`(`ToolContextUnavailableError`,`TOOL_CONTEXT_UNAVAILABLE`)失败,空白命令以 `ToolArgsError`(`INVALID_ARGS`)失败,不接受回显与历史设置的 shell 以 `persistent bash shell did not accept initialization`(`TerminalError`,`INIT_REJECTED`)失败,没有 PTY 后端的组合会激活该工具,但首次调用以 `no PTY backend registered for "shell"` 失败。交互式前台子进程(例如 REPL)只有在后端证明其 stdin 等待时才提前返回部分输出;否则调用一直运行到 `timeoutMs`,随后关闭不确定的 shell 并报告重置。取消也会重置并丢弃结果,即使完整状态标记已经可观察。清理完成后,ToolRuntime 发布 `Error: tool call aborted`,错误码为 `ABORTED`;取消原因不作为命令输出渲染。排队期间被取消的调用不发送命令。
-----
@@ -162,7 +162,7 @@ describe('BashTerminalBackend startup rollback', () => {
spawnError: startupFailure,
cleanupError: cleanupFailure,
errors: [startupFailure, cleanupFailure],
cause: startupFailure,
cause: expect.objectContaining({ errors: [startupFailure, cleanupFailure] }) as AggregateError,
} satisfies Partial<TerminalBackendCleanupError>))
})
+4 -4
View File
@@ -45,11 +45,11 @@
en: a2adfa2708882e19
zh: 002f257fd8c4addb
/deepseek-ai-dsh-terminal/understand-the-implementation/ownership-and-cleanup-rules:
en: b669a68c739b062a
zh: cc641f850905095f
en: 3c977ec7192623b1
zh: 78d14a23e087808a
/deepseek-ai-dsh-terminal/understand-the-implementation/send-reservation:
en: 0b8dc3a382aa7bf2
zh: e6cc6906e01827d2
en: b80a540215116c43
zh: 1dc651d09ceb4b15
/deepseek-ai-dsh-terminal/further-exploration:
en: 8459a3b70a9f8bad
zh: 21372aa8939d2182
+2 -2
View File
@@ -84,12 +84,12 @@ Each published session is a record of its id, owner, optional name, backend type
### Ownership and cleanup rules
- Fencing uses the exact `Agent` object: `hasOwnerActivity(owner)` spans unpublished setup through final close with no publication gap, so lifecycle policy can fence the owner precisely.
- A backend that cannot clean partial startup resources rejects with `TerminalBackendCleanupError`, a `TerminalError` with code `CLEANUP_FAILED` that lists both failures in `errors`; the service retains that failure as tracked owner activity until owner or service disposal consumes and reports it.
- A backend that cannot clean partial startup resources rejects with `TerminalBackendCleanupError`, a `TerminalError` with code `CLEANUP_FAILED` that lists both failures in `errors` and in an `AggregateError` `cause`; the service retains that failure as tracked owner activity until owner or service disposal consumes and reports it.
- Caller cancellation keeps its exact `AbortSignal.reason`; `kill()` and disposal resolve only after the backend's captured process tree is quiescent.
### Send reservation
The service reserves a session synchronously for one active send before returning the operation, including before a background job id becomes visible; a second send fails with `SEND_ACTIVE`, so output and cancellation never cross operation ownership. Failures are `TerminalError` instances (a `HarnessError`); besides the registry codes, a send to a closing session fails with `SESSION_CLOSING`, and backends report `SESSION_EXITED`, `STARTUP_EXITED`, `STARTUP_TIMEOUT`, `NO_SANDBOX`, and `INIT_REJECTED` with the same class.
The service reserves a session synchronously for one active send before returning the operation, including before a background job id becomes visible; a second send fails with `SEND_ACTIVE`, so output and cancellation never cross operation ownership. Failures are `TerminalError` instances (a `HarnessError`); besides the registry codes, a send to a closing session fails with `SESSION_CLOSING`, and backends report `SESSION_EXITED`, `STARTUP_EXITED`, `STARTUP_TIMEOUT`, and `NO_SANDBOX` with the same class. A consumer that drives a shell uses `INIT_REJECTED` when that shell rejects the consumer's setup input.
</details>
+2 -2
View File
@@ -84,12 +84,12 @@ kind: "package-reference"
### 所有权与清理规则
- 限制基于确切的 `Agent` 对象:`hasOwnerActivity(owner)` 覆盖从尚未发布的设置到最终关闭的全过程,没有发布竞态,因此生命周期策略可以精确限制所有者。
- 无法清理部分启动资源的后端会以 `TerminalBackendCleanupError` 拒绝,它是 code 为 `CLEANUP_FAILED` 的 `TerminalError`,并在 `errors` 中列出两个失败;服务会将该失败保留为受跟踪的所有者活动,直到所有者或服务 dispose 消费并报告它。
- 无法清理部分启动资源的后端会以 `TerminalBackendCleanupError` 拒绝,它是 code 为 `CLEANUP_FAILED` 的 `TerminalError`,在 `errors` 和 `AggregateError` 类型的 `cause` 中列出两个失败;服务会将该失败保留为受跟踪的所有者活动,直到所有者或服务 dispose 消费并报告它。
- 调用方取消保留其确切的 `AbortSignal.reason`;`kill()` 与 dispose 只在后端捕获的进程树完全停稳后完成。
### 发送预留
服务在返回操作之前同步为一个活跃发送预留会话,包括在后台任务的 job id 可见之前;第二次发送会以 `SEND_ACTIVE` 失败,因此输出与取消永远不会跨操作所有权。失败均为 `TerminalError`(一种 `HarnessError`);除注册表 code 外,向正在关闭的会话发送会以 `SESSION_CLOSING` 失败,后端以同一类报告 `SESSION_EXITED`、`STARTUP_EXITED`、`STARTUP_TIMEOUT`、`NO_SANDBOX` 与 `INIT_REJECTED`。
服务在返回操作之前同步为一个活跃发送预留会话,包括在后台任务的 job id 可见之前;第二次发送会以 `SEND_ACTIVE` 失败,因此输出与取消永远不会跨操作所有权。失败均为 `TerminalError`(一种 `HarnessError`);除注册表 code 外,向正在关闭的会话发送会以 `SESSION_CLOSING` 失败,后端以同一类报告 `SESSION_EXITED`、`STARTUP_EXITED`、`STARTUP_TIMEOUT` 与 `NO_SANDBOX`。通过服务驱动 shell 的消费方在该 shell 拒绝自身的初始化输入时使用 `INIT_REJECTED`。
</details>
+13 -9
View File
@@ -4,13 +4,14 @@ import { HarnessError } from '@deepseek-ai/dsh-error'
/**
* Machine-routable PTY service and backend failures. Backends use
* `INIT_REJECTED` when the shell rejects its setup input, `NO_SANDBOX` when a
* confined mode has no sandbox provider, `SESSION_CLOSING` and
* `SESSION_EXITED` for an operation on a closing or exited session,
* `STARTUP_EXITED` or `STARTUP_TIMEOUT` when the shell exits or misses
* readiness during startup, and `CLEANUP_FAILED` (always as
* `NO_SANDBOX` when a confined mode has no sandbox provider,
* `SESSION_CLOSING` and `SESSION_EXITED` for an operation on a closing or
* exited session, `STARTUP_EXITED` or `STARTUP_TIMEOUT` when the shell exits
* or misses readiness during startup, and `CLEANUP_FAILED` (always as
* {@link TerminalBackendCleanupError}) when startup failed and the partial
* resources it left could not be cleaned.
* resources it left could not be cleaned. A consumer that drives a shell
* through the service uses `INIT_REJECTED` when that shell rejects the
* consumer's own setup input.
*/
export type TerminalErrorCode =
| 'CLEANUP_FAILED'
@@ -48,8 +49,9 @@ export class TerminalError extends HarnessError {
/**
* Backend-reported failure to clean partial resources after unpublished setup
* failed; code `CLEANUP_FAILED`. Backend-owned resources may still be alive.
* `errors` lists both failures in the order `AggregateError` would, and
* `cause` is the setup failure.
* `errors` lists the setup failure, then the cleanup failure; `cause` is an
* `AggregateError` of the same two failures, so renderers that follow `cause`
* report both.
*/
export class TerminalBackendCleanupError extends TerminalError {
/** The setup failure followed by the cleanup failure. */
@@ -63,7 +65,9 @@ export class TerminalBackendCleanupError extends TerminalError {
readonly spawnError: unknown,
readonly cleanupError: unknown,
) {
super('PTY backend startup and cleanup both failed', 'CLEANUP_FAILED', { cause: spawnError })
super('PTY backend startup and cleanup both failed', 'CLEANUP_FAILED', {
cause: new AggregateError([spawnError, cleanupError], 'PTY backend startup and cleanup both failed'),
})
this.name = 'TerminalBackendCleanupError'
this.errors = [spawnError, cleanupError]
}