2026-07-15 23:11:25 -07:00
# 用户审批
[English ](approval.md ) | 中文
2026-08-13 00:36:22 +08:00
[dsh-user-approval ](../../packages/interaction/user-approval ) 的用户审批 seam 回答一个问题:这个具体操作是否可以继续?它拥有共享的请求/结果词汇、`ctx.approval` 分发服务、`approval/request` 应答者 waterfall( 瀑布式事件) 、仅记录日志的审计事件对, 以及按会话的 `ask` /`never` 策略。UI 通道可以提供人类应答者;[ACP( Agent Client Protocol) 自动化桥接层 ](../../packages/acp/acp )为其拥有的 agent( 智能体) 提供一次性机器决策。调用方如 [dsh-tools ](../../packages/core/tools ) 和 [dsh-tool-bash ](../../packages/shell/tool-bash ) 消费闭合的结果,除非结果为 `allowed-once` ,否则一律拒绝。
2026-07-15 23:11:25 -07:00
refactor(packages): dissolve ui/ and rename sdk/ to scaffold/
git mv per the regrouping RFC: the five human-collaboration seams and
tui join packages/interaction/, app-boot becomes packages/boot/, and
jsonrpc joins the renamed scaffold/ (formerly sdk/) as its server half
beside client/protocol/create-sdk/helper/scripts/telemetry, whose
folders drop the legacy sdk- prefix. Three new group README triplets
replace the ui/ and sdk/ ones; tsconfig references/paths/globs,
knip keys, vitest globs, gate scripts, catalogs, docs, and the
lockfile follow. Adds the four settled FIXME rename markers
(dsh-sdk-server, dsh-sdk-telemetry, dsh-sdk-helper, dsh-sdk-scripts).
The scaffold folders diverge from their npm names until those renames
land, so tsconfig.base.json maps the three affected names explicitly
beside the group wildcard. Also repairs two pre-existing stale-path
classes the strengthened sweep surfaced: docs/web-styling.md's retired
web-ui host package and type-model spec fixture-literal joins.
app-boot's three Loader-composition specs time out at the default 5s
under full-suite parallel load on this filesystem (pre-existing;
pass isolated with --testTimeout=30000); interaction/scaffold/boot
suites otherwise green (687 passed).
2026-07-30 03:13:49 +08:00
源码:[`packages/interaction/user-approval/src/index.ts` ](../../packages/interaction/user-approval/src/index.ts )
2026-07-15 23:11:25 -07:00
## 标识与结果
2026-08-04 17:36:14 +08:00
每个请求都会获得一个全新的 `ApprovalRequestId` 。该品牌类型将 `approval/asked` 与 `approval/decided` 审计事件配对,同时不会让审批 id 与工具调用 id 或 agent/会话 id 互换。
2026-07-15 23:11:25 -07:00
```ts type-equiv
2026-07-22 22:58:05 +08:00
/**
* Pairs one `approval/asked` audit event with its `approval/decided` .
* Service-issued (one fresh id per {@link ApprovalService.request} call).
*/
2026-07-15 23:11:25 -07:00
type ApprovalRequestId = Branded< 'ApprovalRequestId'>
```
2026-08-04 17:36:14 +08:00
`ApprovalOutcome` 是闭合的,且失败时拒绝。`allowed-once` 仅授权所询问的那一个操作;调用方对 `rejected` 、`cancelled` 和 `unavailable` 均执行拒绝。缺失、不负责该请求、抛异常或不合规的应答者会产生 `unavailable` ,而非放行。
2026-07-15 23:11:25 -07:00
```ts type-equiv
2026-07-22 22:58:05 +08:00
/**
* Closed approval outcomes: a one-shot grant, explicit rejection, withdrawn
* request, or unavailable answerer. Callers fail closed on `unavailable` .
*/
2026-07-15 23:11:25 -07:00
type ApprovalOutcome = 'allowed-once' | 'rejected' | 'cancelled' | 'unavailable'
```
## 按会话策略
2026-07-22 03:06:01 -07:00
`ApprovalPolicy` 决定在交互式应答者运行之前发生什么。`ask` 委托给组合的应答者链,链的无应答默认值为 `unavailable` ; `never` 确定性地返回 `rejected` ,不分发任何应答者。生效值为会话日志中最后一条 `approval/policy` 事件,回退到服务配置。`setApprovalPolicy(session, policy)` 是唯一的写入路径,因此回放能重建覆盖值。
2026-07-15 23:11:25 -07:00
```ts type-equiv
2026-07-22 22:58:05 +08:00
/**
* A session's approval policy — what happens to an {@link ApprovalService}
* ask BEFORE any interactive answerer sees it:
*
* - `'ask'` (the default) — delegate to the composed answerers; with none
docs: purge chain-of-thought leakage from prose
Delete design-session citations (decision/audit/plan ordinals, stack
positions), change narration, review choreography, and reviewer-addressed
justification from comments, JSDoc, docs, READMEs, Agent Notes, tests, and
generator templates; restate every affected fact as current-state contract
prose. Fix generated docs at their sources and regenerate the catalogs and
cordis-surface regions; re-paste type-equiv blocks; update every bilingual
counterpart and re-record the pairs. Record the citation rule in the
committed-artifact-citations Agent Note.
2026-08-09 15:09:19 +08:00
* composed the chain falls through to the fail-closed `'unavailable'` .
2026-07-22 22:58:05 +08:00
* - `'never'` — never prompt anyone: every ask resolves `'rejected'`
* deterministically. The strict headless stance (CI, unattended runs) and
2026-07-30 22:09:15 +08:00
* the policy whose outcome is knowable without asking.
2026-07-22 22:58:05 +08:00
*/
2026-07-15 23:11:25 -07:00
type ApprovalPolicy = 'ask' | 'never'
```
2026-08-12 09:59:19 +08:00
两种策略都会将各自完整的当前含义贡献给缓存安全的运行时上下文快照。带来源的 `user/message` 是持久化且模型可见的输入;审批状态变化时,会在保留的历史后追加一份新的完整快照,而不改写请求头中的系统提示词。
2026-07-15 23:11:25 -07:00
## 审批请求
2026-08-12 12:30:19 +08:00
`ApprovalRequest` 以足够精确的方式标识 agent 和工具操作,以便路由和审计该问题。它有意省略工具参数:应答者通过 `callId` 将提示附加到已流式输出的工具调用上,而非渲染另一份可能漂移的副本。
2026-07-15 23:11:25 -07:00
```ts type-equiv
2026-07-22 22:58:05 +08:00
/**
* Readonly same-process permission question. `callId` links to an already
* presented tool call, so arguments are not duplicated here.
*/
2026-08-23 06:14:32 +08:00
interface ApprovalRequest extends ApprovalRequestEvent {
2026-07-15 23:11:25 -07:00
/**
* The agent on whose behalf the question is asked. Routes the question (a
* UI answerer only answers for agents it owns) and receives the audit
* events on its session log.
*/
readonly agent: Agent
/** The tool the question is about (presentation and audit). */
readonly toolName: string
/**
* The exact tool call being decided, when the asker has one — lets a UI
* attach the prompt to the tool call it already streamed.
*/
readonly callId?: CallId
/** The asker's human-readable explanation of WHY it is asking. */
readonly reason?: string
/**
* Aborting withdraws the question: the request settles `'cancelled'`
* immediately and a late answer from a still-pending answerer is discarded.
*/
readonly signal?: AbortSignal
}
```
## 分发与审计
2026-08-12 09:59:19 +08:00
`ctx.approval.request(req)` 要求发起请求的会话处于一个尚未结束的轮次内。它追加 `approval/asked` ,获取一个结果,追加对应的 `approval/decided` ,然后以该结果完成。`never` 策略在服务内部、waterfall 分发之前强制执行,因此即使后来以 `prepend` 注册的应答者也无法绕过它。应答者在负责处理该请求时返回结果,否则调用 `next()` 委托;第一个应答占据唯一的决策槽位。
2026-07-15 23:11:25 -07:00
2026-07-30 22:09:15 +08:00
审计事件仅写入日志,不进入模型 transcript( 文本记录) 。模型可见的行为是调用方派生的工具结果与当前运行时上下文快照。服务 dispose( 资源释放) 时会移除其上下文贡献; 应答者监听器独立地通过 effect 绑定到其所属插件。
2026-07-30 21:40:58 +08:00
<!-- BEGIN GENERATED cordis - surface (gen - cordis - catalog.ts) — do not edit between markers -->
< a id = "cordis-surface" > < / a >
2026-07-24 19:54:25 +08:00
## Cordis API
2026-07-30 21:40:58 +08:00
2026-08-18 21:02:50 +08:00
Generated from source by `scripts/gen-cordis-catalog.ts` (verified fresh by `pnpm run verify-cordis-catalog` in doc-sync; regenerate with `pnpm run gen-cordis-catalog` ) — the language sides differ only in locale-specific paired document paths. Signature blocks use a `ts cordis-catalog` fence and keep the original source JSDoc; dispatch modes are defined in the [primer ](../cordis-primer.zh.md#dispatch-modes ), and the framework-inherited `ctx` API lives in [cordis-api/inherited.md ](../cordis-api/inherited.md ).
2026-07-30 21:40:58 +08:00
< a id = "ctxapproval--approvalservice" > < / a >
### `ctx.approval` — `ApprovalService`
Approval service that applies session policy before answerers and logs every ask/outcome pair to the requesting session. It exposes deterministic policy changes to the model through the runtime-context snapshot and switch notices.
```ts cordis-catalog
/**
* Switch one live agent's policy and queue the transition for its next model
* step. Session initialization uses {@link setApprovalPolicy} directly
* because there is no previously visible policy to change.
* @param agent - the live agent whose policy is changing.
* @param policy - the new effective policy.
*/
setPolicy(agent: Agent, policy: ApprovalPolicy): void
/**
* Ask the composed answerers to decide one readonly same-process request.
* The service borrows the request, agent, session, and live signal directly.
* The request requires an open turn because the audit pair must be enclosed
* by the durable log's commit/replay boundary; an idle ask rejects before
* appending anything. The answerer phase always produces an outcome: an
* aborted signal yields `'cancelled'` , a missing or throwing answerer yields
* `'unavailable'` (fail closed), and a rogue non-vocabulary return value is
* normalized to `'unavailable'` . A failure that prevents either audit append
* from committing still rejects because returning an unlogged decision would
* violate the pair. Session contains post-commit observer failures, so an
* authoritative append cannot reject the request or suppress its matching
* audit event.
* @param req - the pending decision (agent, tool identity, reason, signal).
* @returns the closed outcome; `'allowed-once'` is the only grant.
* @throws when no turn is open or either audit event fails before the session
* append commit point.
*/
async request(req: ApprovalRequest): Promise< ApprovalOutcome >
/**
* Read the session override without applying the configured default.
* @param session - session whose log supplies the override.
* @returns the last logged policy, or `undefined` without one.
*/
overrideOf(session: Session): ApprovalPolicy | undefined
```
2026-08-18 19:00:37 +08:00
Types: [Agent ](core.zh.md ) · [Session ](session.zh.md )
2026-07-30 21:40:58 +08:00
2026-08-04 11:38:42 +08:00
Source: [`packages/interaction/user-approval/src/index.ts` ](../../packages/interaction/user-approval/src/index.ts )
2026-07-30 21:40:58 +08:00
< a id = "approval-events" > < / a >
### `approval/*` events
< a id = "approvalrequest--waterfall" > < / a >
#### `approval/request` — waterfall
2026-08-23 06:14:32 +08:00
Ask composed answerers for one decision. Return an outcome to claim the request or call `next()` to delegate. Scope-filtered dispatch (`@deepseek-ai/dsh-scope` ): agent-scoped listeners receive only that agent.
2026-07-30 21:40:58 +08:00
```ts cordis-catalog
/**
* Ask composed answerers for one decision. Return an outcome to claim the
2026-08-23 06:14:32 +08:00
* request or call `next()` to delegate. Scope-filtered dispatch
* (`@deepseek-ai/dsh-scope` ): agent-scoped listeners receive only that agent.
* @param req - pending approval request.
2026-07-30 21:40:58 +08:00
* @mode waterfall
*/
2026-08-23 06:14:32 +08:00
'approval/request'( this: Scoped< Agent > , req: ApprovalRequestEvent, next: () => Promise< ApprovalOutcome > , ): Promise< ApprovalOutcome >
2026-07-30 21:40:58 +08:00
```
2026-08-23 06:14:32 +08:00
Types: [Agent ](core.zh.md ) · [Scoped ](scope.zh.md )
2026-07-30 21:40:58 +08:00
2026-08-23 06:14:32 +08:00
Source: [`packages/interaction/user-approval/src/types.ts` ](../../packages/interaction/user-approval/src/types.ts )
2026-07-30 21:40:58 +08:00
<!-- END GENERATED cordis - surface -->