@zhushanwen/subagent-engine-sdk 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,21 @@
1
1
  // src/protocol/contract-types.ts
2
2
  //
3
3
  // 引擎面契约类型 SSOT(协议两侧不许各写一份;core 反向 re-export 保上层消费面)。
4
- // 设计权威源:docs/design/subagent-engine-protocolization.md §3.5.1 D7 类型闭包表 +
4
+ // 设计权威源:docs/architecture/subagent-engine-protocolization.md §3.5.1 D7 类型闭包表 +
5
5
  // impl-plan §2.1「类型闭包处置」。
6
6
  //
7
7
  // 搬运口径(逐字对照,结构等价、零 core import):
8
8
  // - AgentEvent / AgentUsage / AgentUsageTotal / ToolCallResult / ToolCall /
9
- // InternalToolCall / Turn ← core execution/types.ts(2026-09-09 实测 :164-:313)
9
+ // InternalToolCall / Turn ← core execution/assembly/types.ts(2026-09-09 实测 :164-:313)
10
10
  // - ReplayedTurn / SessionView / EngineHandleData / EngineCapabilities / ProbeReport /
11
11
  // AgentOutcome ← core execution/engine/types.ts
12
12
  // - AgentFailureKind / AgentOutcomeUsage(core 名 AgentUsage,orchestration 版)/
13
13
  // ToolCallEntry / AgentCallOpts 子集 ← core orchestration/models/types.ts
14
- // - WorktreeHandle ← core execution/types.ts:349(SDK 结构等价副本——设计 §3.5.1
14
+ // - WorktreeHandle ← core execution/assembly/types.ts:349(SDK 结构等价副本——设计 §3.5.1
15
15
  // 点名「AgentCallOpts.worktree 的 WorktreeHandle 即这类副本」)
16
16
  //
17
17
  // [H1] InteractAction / InteractResult 已随 chat-run 统一退役(U5 删除;
18
- // docs/design/subagent-chat-run-unification.md §3.3 D5——续聊统一为新 run + resume)。
18
+ // docs/architecture/subagent-chat-run-unification.md §3.3 D5——续聊统一为新 run + resume)。
19
19
  //
20
20
  // core 域类型(ExecutionRecord / Turn 的宿主内部态消费)留 core;SDK 侧一切类型为
21
21
  // 结构等价形态,漂移由双向可赋值断言(AssertMutuallyAssignable)在 typecheck 期抓出
@@ -44,7 +44,7 @@ export type AssertMutuallyAssignable<A, B> = [A] extends [B]
44
44
  // 事件面(AgentEvent 及其字段型)
45
45
  // ============================================================
46
46
 
47
- /** token 用量(message_end 单条消息增量)。← core execution/types.ts AgentUsage。 */
47
+ /** token 用量(message_end 单条消息增量)。← core execution/assembly/types.ts AgentUsage。 */
48
48
  export interface AgentUsage {
49
49
  input: number;
50
50
  output: number;
@@ -100,7 +100,7 @@ export interface Turn {
100
100
 
101
101
  /**
102
102
  * 引擎事件(9 种,协议 event.params.event 逐字序列化——「事件与 handle 序列化逐字
103
- * 兼容」不变量 3 的类型面)。语义锚点 = pi(ACP 词汇对照见 core execution/types.ts 注释)。
103
+ * 兼容」不变量 3 的类型面)。语义锚点 = pi(ACP 词汇对照见 core execution/assembly/types.ts 注释)。
104
104
  *
105
105
  * activity = 纯活性信号:双侧 reducer no-op、不开 turn、不写状态、不落 journal
106
106
  * (core journal-wiring 对其豁免 append),只承诺「引擎活跃时周期性出现」——供宿主
@@ -131,9 +131,11 @@ export interface EngineHandleData {
131
131
  engineId: string;
132
132
  /** 引擎自定义定位键值。pi = { recordId?, sessionFile? };zcode = { sessionId, dbPath }。 */
133
133
  sessionRef: Record<string, string>;
134
- /** 隔离池定位。pi 无池化恒 'shared'。 */
135
- poolKey: string;
136
- /** journal 绝对路径(read 第②级数据源;宿主读前校验前缀白名单)。缺省 = 无 journal。 */
134
+ /**
135
+ * journal 绝对路径(read 第②级数据源;宿主读前校验前缀白名单)。缺省 = 无 journal。
136
+ * [池抽象降级 2026-09-13] 原 poolKey 字段已删除——两引擎 poolKey 恒 'shared'
137
+ * (SDK SHARED_POOL_KEY),journal 固定落 engines/<engineId>/shared/,字段零信息量。
138
+ */
137
139
  journalPath?: string;
138
140
  /** probe 实测版本(漂移排查锚点)。 */
139
141
  engineVersion?: string;
@@ -152,8 +154,6 @@ export interface EngineHandleData {
152
154
  export interface ResumeAnchor {
153
155
  /** 引擎定位键(pi = { recordId?, sessionFile? };zcode = { sessionId, dbPath })。 */
154
156
  sessionRef: Record<string, string>;
155
- /** 隔离池定位(锚点补全 handle 重建所需;pi 无池化恒 'shared')。 */
156
- poolKey: string;
157
157
  /** journal 绝对路径(read 降级链第②级数据源;无 journal 缺省)。 */
158
158
  journalPath?: string;
159
159
  }
@@ -199,8 +199,12 @@ export interface EngineCapabilities {
199
199
  * [H1 D5 语义收窄] resume 能力位(chat 续聊 = 新 run + resume 锚点的承载前提;
200
200
  * 原名字沿用——conversation 位保留、语义从「interact 长驻控制面」收窄为
201
201
  * 「resume 续聊能力」,gate 判据与消费方不变)。
202
+ * [U6 / 永久会话模型 §3.2.6 要点 4] 新值 "cold" = 冷恢复会话:resume 读通道取
203
+ * 结构化历史 + 新 session 注入(无热 steering)。gate 判据仍是 `=== "unsupported"`
204
+ * 拒绝——"cold" 在 gate 面与 "native" 等价放行,差异只在续聊形态(新 session
205
+ * 注入 vs 原地续写)。
202
206
  */
203
- conversation: "native" | "unsupported";
207
+ conversation: "native" | "cold" | "unsupported";
204
208
  /** 决定 persona 路由策略(file/flag/prompt 通道)。 */
205
209
  personaInjection: "file" | "flag" | "prompt";
206
210
  /** 粗粒度引擎:GUI 显示降级为阶段态。 */
@@ -258,7 +262,7 @@ export interface ToolCallEntry {
258
262
  input: string;
259
263
  }
260
264
 
261
- /** worktree 句柄(结构等价副本;core 权威定义在 execution/types.ts:349)。 */
265
+ /** worktree 句柄(结构等价副本;core 权威定义在 execution/assembly/types.ts:349)。 */
262
266
  export interface WorktreeHandle {
263
267
  /** checkout 目录(子 agent 工作目录)。 */
264
268
  readonly path: string;
@@ -1,7 +1,7 @@
1
1
  // src/protocol/engine-protocol.ts
2
2
  //
3
3
  // 引擎协议 v1 版本常量与协商(W1 契约根)。设计权威源:
4
- // docs/design/subagent-engine-protocolization.md §3.3 + impl-plan §2.1。
4
+ // docs/architecture/subagent-engine-protocolization.md §3.3 + impl-plan §2.1。
5
5
  //
6
6
  // 传输 = stdio NDJSON(每行一个 JSON 对象)。stdout 独占协议帧;stderr 常驻排空
7
7
  // (内存环形缓冲尾 400 字符,崩溃现场由 engine_crashed 携带,宿主侧不落盘)。
@@ -21,7 +21,7 @@
21
21
  // union,旧宿主 runtime 对其 no-op(reducer default 分支安全落空 / journal
22
22
  // 豁免面不感知),协议版本维持 1、不 bump。
23
23
  //
24
- // [H1 双键过渡(chat-run 统一,docs/design/subagent-chat-run-unification.md §3.3
24
+ // [H1 双键过渡(chat-run 统一,docs/architecture/subagent-chat-run-unification.md §3.3
25
25
  // D3 + §5 U1 行)][H1 U6 已切换]:run.params.resume 曾与原 run.params.chat 载荷
26
26
  // 同形并存(additive 可选,协议版本维持 1);U1 只加键——U2-U5 过渡期 core 恒
27
27
  // 构造旧 `chat` 键、pi 引擎恒读 `ctx.chat`。U6 已单批同时切换写端(core 构造
@@ -83,9 +83,9 @@ export const ENGINE_EVENT_COALESCE_DEFAULT = "0";
83
83
 
84
84
  /**
85
85
  * 反向请求超时二分(帧④注释,R9-2):
86
- * - 数据面类(host/log / host/streamDelta / host/poolResolved / host/handleReady /
86
+ * - 数据面类(host/log / host/streamDelta / host/handleReady /
87
87
  * host/childSpawned / host/childStateChanged):10s 未答 = 引擎故障;
88
- * - 人机交互类(host/askUser / host/permission):不设统一超时——core 先回 {ack:true},
88
+ * - 人机交互类(host/askUser):不设统一超时——core 先回 {ack:true},
89
89
  * 结果异步到达;按 ADR-0047「静默 ≠ 卡死」用无进展检测/用户取消,不据此判引擎故障。
90
90
  */
91
91
  export type ReverseRequestTimeoutClass = "data-plane" | "interaction";
@@ -82,13 +82,11 @@ export interface ReverseRequestFrame {
82
82
  export type ReverseResponseResult =
83
83
  /** 数据面类:处理确认。 */
84
84
  | { ok: true }
85
- /** 人机交互类(askUser/permission)两阶段第一阶段:已受理,结果异步到达(R9-2)。 */
85
+ /** 人机交互类(askUser)两阶段第一阶段:已受理,结果异步到达(R9-2)。 */
86
86
  | { ack: true }
87
87
  /** 未实现的交互能力(设计:未实现回 unsupported,引擎自行降级)。 */
88
88
  | { unsupported: true };
89
89
 
90
- export type AnyFrame = RequestFrame | ResponseFrame | NotificationFrame | ReverseRequestFrame;
91
-
92
90
  // ============================================================
93
91
  // 判别守卫(W2 行解析器消费;结构判定,不抛错)
94
92
  // ============================================================
@@ -5,7 +5,7 @@
5
5
  //
6
6
  // [v1.x 增量(chat-domain 设计 §3.2 D1-A)]:增量以可选参数形态落在 run.params.chat
7
7
  // (会话形态参数 + 冷续 resume 锚点),major 不 bump。[H1] chat-run 统一后续聊 =
8
- // 新 run + resume 锚点(docs/design/subagent-chat-run-unification.md §3.3 D5/D7),
8
+ // 新 run + resume 锚点(docs/architecture/subagent-chat-run-unification.md §3.3 D5/D7),
9
9
  // 既有 interact 方法已随 U5 删除,方法集收敛为 9 个。
10
10
  //
11
11
  // 应答面补充约定(设计 §3.3):initialize 应答仅诊断(与 manifest 不一致 → warn 留痕,
@@ -26,7 +26,7 @@ import type {
26
26
 
27
27
  /**
28
28
  * 正向方法名联合(恰好 9 个;PROTOCOL_METHODS 常量数组与之同源互证)。
29
- * [H1] `interact` 成员已随 chat-run 统一退役(docs/design/subagent-chat-run-unification.md
29
+ * [H1] `interact` 成员已随 chat-run 统一退役(docs/architecture/subagent-chat-run-unification.md
30
30
  * §3.3 D5:续聊轮统一为「新 run + resume 锚点」,U5 删除)。
31
31
  */
32
32
  export type ProtocolMethod =
@@ -61,8 +61,6 @@ export const PROTOCOL_METHODS = [
61
61
  * run 上下文(RunContext 字段映射的协议承载,设计 §3.3 RunContext 映射表)。
62
62
  */
63
63
  export interface RunContextParams {
64
- /** 隔离池归属(journal 归属错 = 缺失后果)。 */
65
- poolKey: string;
66
64
  /** 任务工作目录(worktree 隔离时 = worktree 路径)。 */
67
65
  cwd: string;
68
66
  /** 请求模型 ref(未传 = 引擎缺省模型)。 */
@@ -96,7 +94,7 @@ export interface RunContextParams {
96
94
  // ============================================================
97
95
 
98
96
  /**
99
- * [H1] run 的 resume 会话形态参数(设计 docs/design/subagent-chat-run-unification.md
97
+ * [H1] run 的 resume 会话形态参数(设计 docs/architecture/subagent-chat-run-unification.md
100
98
  * §3.3 D3 + §5 U1/U6 行):原 RunChatParams(v1.x chat 会话形态参数)的泛化改名终态,
101
99
  * 载荷同形(recordId + resume 锚点,ResumeAnchor 不变),仅键名从「chat 会话形态」
102
100
  * 泛化为「resume 续聊」。
@@ -1,10 +1,14 @@
1
1
  // src/protocol/reverse-channels.ts
2
2
  //
3
- // 8 反向通道(引擎 → core,帧④,必须应答)载荷与超时二分。设计权威源:
3
+ // 6 反向通道(引擎 → core,帧④,必须应答)载荷与超时二分。设计权威源:
4
4
  // 设计 §3.3 方法集表 host/* 行 + impl-plan §2.1「8 反向通道」与「反向请求超时二分」。
5
5
  // [H1] chat 域 v1.x 增量曾新增的第 9 通道(轮次相位帧)已随 chat-run 统一退役
6
- // (docs/design/subagent-chat-run-unification.md §3.3 D5,U5 删除)——轮次终态
7
- // 改由 run 应答(agent_settled resolve)承载,通道集收敛回 8 个。
6
+ // (docs/architecture/subagent-chat-run-unification.md §3.3 D5,U5 删除)——轮次终态
7
+ // 改由 run 应答(agent_settled resolve)承载。
8
+ // [池抽象降级 2026-09-13] 原 host/poolResolved 通道(第 8 条)随 poolKey 协议面退役
9
+ // 一并删除(两引擎 poolKey 恒 'shared'、journal 落盘路径固定,回调零信息量)。
10
+ // [permission 通道退役 2026-09-13] host/permission 骨架删除(两引擎 permissionMode=
11
+ // native 零 emit、core 零注入——未接线死通道),通道集收敛为 6 个。
8
12
  //
9
13
  // 应答约定:数据面类回 {ok:true}(REVERSE_REQUEST_TIMEOUT_MS=10s 未答 = 引擎故障 →
10
14
  // 杀进程 + 在途 run 失败);人机交互类走 ack 两阶段——先回 {ack:true},结果异步到达
@@ -15,14 +19,12 @@ import type { ReverseRequestTimeoutClass } from "./engine-protocol.ts";
15
19
  import type { UiRequest, UiResponse } from "../ui-types.ts";
16
20
 
17
21
  /**
18
- * 反向通道名联合(恰好 8 个;REVERSE_CHANNELS 常量数组与之同源互证)。
22
+ * 反向通道名联合(恰好 6 个;REVERSE_CHANNELS 常量数组与之同源互证)。
19
23
  */
20
24
  export type ReverseChannel =
21
25
  | "host/log"
22
26
  | "host/askUser"
23
- | "host/permission"
24
27
  | "host/streamDelta"
25
- | "host/poolResolved"
26
28
  | "host/handleReady"
27
29
  | "host/childSpawned"
28
30
  | "host/childStateChanged";
@@ -33,9 +35,7 @@ export type ReverseChannel =
33
35
  export const REVERSE_CHANNELS = [
34
36
  "host/log",
35
37
  "host/askUser",
36
- "host/permission",
37
38
  "host/streamDelta",
38
- "host/poolResolved",
39
39
  "host/handleReady",
40
40
  "host/childSpawned",
41
41
  "host/childStateChanged",
@@ -48,12 +48,10 @@ export const REVERSE_CHANNELS = [
48
48
  export const REVERSE_CHANNEL_TIMEOUT_CLASS: Record<ReverseChannel, ReverseRequestTimeoutClass> = {
49
49
  "host/log": "data-plane",
50
50
  "host/streamDelta": "data-plane",
51
- "host/poolResolved": "data-plane",
52
51
  "host/handleReady": "data-plane",
53
52
  "host/childSpawned": "data-plane",
54
53
  "host/childStateChanged": "data-plane",
55
54
  "host/askUser": "interaction",
56
- "host/permission": "interaction",
57
55
  };
58
56
 
59
57
  // ============================================================
@@ -78,62 +76,25 @@ export interface HostAskUserParams {
78
76
  /** host/askUser 的最终结果(ack 两阶段第二阶段,异步应答帧②的 result)。 */
79
77
  export type HostAskUserResult = UiResponse;
80
78
 
81
- /**
82
- * host/permission:权限询问(引擎请求宿主裁决工具执行)。
83
- * v1 骨架字段(设计未钉死载荷细节,细化归各引擎提取设计;W2 实装 core 应答端时
84
- * 若需扩展走 additive 演进)。
85
- */
86
- export interface HostPermissionParams {
87
- runId: string;
88
- toolName: string;
89
- args?: unknown;
90
- /** 请求方向引擎给出的原因/说明(展示用)。 */
91
- reason?: string;
92
- }
93
-
94
- /** host/permission 两阶段结果。 */
95
- export type HostPermissionResult = { approved: boolean } | { unsupported: true };
96
-
97
79
  /**
98
80
  * host/streamDelta:UI 实时通道(双通道之一;与 event 通知并行的渲染加速面)。
99
81
  *
100
- * [H1 U6 现行语义] 关联键恒 runId(run 域轮与 chat 续聊轮已统一为 run——续聊轮
101
- * 是携带 resume 锚点的新 run,runId 由 core 在 run 帧分配)。recordId 键分支是
102
- * v1.x 旧引擎的兼容载荷形态(存量兼容读,现行引擎不写)。
103
- * 两键互斥(undefined 孪生位防双填),消费侧经 isHostStreamDeltaParams 收窄。
82
+ * 关联键恒为 runId(runId 由 core 在 run 帧分配);[H1] chat 续聊轮经 resume run
83
+ * 复用 runId 关联(原 recordId 关联键随 interact 面退役删除)。
104
84
  */
105
- export type HostStreamDeltaParams =
106
- | { runId: string; recordId?: undefined; delta: string }
107
- | { recordId: string; runId?: undefined; delta: string };
108
-
109
- /** streamDelta 载荷结构判定(关联键互斥 + delta 形状;消费侧共用,防双侧各写一份)。 */
110
- export function isHostStreamDeltaParams(value: unknown): value is HostStreamDeltaParams {
111
- if (typeof value !== "object" || value === null) return false;
112
- const v = value as Record<string, unknown>;
113
- const hasRunId = typeof v.runId === "string";
114
- const hasRecordId = typeof v.recordId === "string";
115
- // 恰一键:无键 / 双键 / 键非 string 都拒
116
- if (hasRunId === hasRecordId) return false;
117
- return typeof v.delta === "string";
118
- }
119
-
120
- /**
121
- * host/poolResolved:journal 落盘路径单一权威(契约:必须在首个事件 emit 前调用——
122
- * 否则 journal 归属错)。载荷 = core onPoolResolved(poolKey) 的 runId 关联形态。
123
- */
124
- export interface HostPoolResolvedParams {
85
+ export interface HostStreamDeltaParams {
125
86
  runId: string;
126
- poolKey: string;
87
+ delta: string;
127
88
  }
128
89
 
129
90
  /**
130
91
  * host/handleReady:运行中句柄回填(core onHandleReady 语义:session/create 应答后、
131
92
  * 早于 run resolve;AGENTS.md 关键规则 9「重开 session 仍可见」的前提)。
93
+ * [池抽象降级 2026-09-13] 原 poolKey 字段已随协议面 poolKey 退役删除。
132
94
  */
133
95
  export interface HostHandleReadyParams {
134
96
  runId: string;
135
97
  sessionRef: Record<string, string>;
136
- poolKey: string;
137
98
  }
138
99
 
139
100
  /**
@@ -160,27 +121,3 @@ export interface HostChildStateChangedParams {
160
121
  exitCode?: number;
161
122
  signal?: string;
162
123
  }
163
-
164
- /** 通道 → 载荷类型映射。 */
165
- export interface ReverseChannelParamsMap {
166
- "host/log": HostLogParams;
167
- "host/askUser": HostAskUserParams;
168
- "host/permission": HostPermissionParams;
169
- "host/streamDelta": HostStreamDeltaParams;
170
- "host/poolResolved": HostPoolResolvedParams;
171
- "host/handleReady": HostHandleReadyParams;
172
- "host/childSpawned": HostChildSpawnedParams;
173
- "host/childStateChanged": HostChildStateChangedParams;
174
- }
175
-
176
- /** 通道 → 异步/同步结果类型映射(ack 两阶段通道的第二阶段 result)。 */
177
- export interface ReverseChannelResultMap {
178
- "host/log": { ok: true };
179
- "host/askUser": HostAskUserResult;
180
- "host/permission": HostPermissionResult;
181
- "host/streamDelta": { ok: true };
182
- "host/poolResolved": { ok: true };
183
- "host/handleReady": { ok: true };
184
- "host/childSpawned": { ok: true };
185
- "host/childStateChanged": { ok: true };
186
- }
@@ -12,7 +12,6 @@
12
12
  // 帧校验的目的是行解析器快速拒格式坏帧,不做深校验(深校验成本高于收益,坏载荷
13
13
  // 由消费方结构化报错)。
14
14
 
15
- import type { ReverseChannel } from "./reverse-channels.ts";
16
15
  import { REVERSE_CHANNELS } from "./reverse-channels.ts";
17
16
  import { PROTOCOL_METHODS } from "./methods.ts";
18
17
 
@@ -151,16 +150,13 @@ export const FORBIDDEN_CREDENTIAL_KEY_FRAGMENTS = [
151
150
  "secret",
152
151
  ] as const;
153
152
 
154
- /** 反向通道名词表导出(schema enum 与 W2 路由同源)。 */
155
- export const REVERSE_CHANNEL_NAMES: readonly ReverseChannel[] = REVERSE_CHANNELS;
156
-
157
153
  // ============================================================
158
154
  // [H1] run 会话形态参数载荷 schema(resume 单键校验权威)
159
155
  // ============================================================
160
156
 
161
157
  /**
162
158
  * run.params 的会话形态参数载荷 schema(draft-07 深载荷片段)。设计权威源:
163
- * docs/design/subagent-chat-run-unification.md §3.3 D3 + §5 U1/U6 行。
159
+ * docs/architecture/subagent-chat-run-unification.md §3.3 D3 + §5 U1/U6 行。
164
160
  *
165
161
  * [H1 U6 已切换] 键切换单批完成后本 schema 面向唯一键 `resume`(RunResumeParams:
166
162
  * recordId + 可选 ResumeAnchor),形状不变。帧级 schema(requestFrameSchema.params)
@@ -178,11 +174,10 @@ export const runSessionParamsSchema = {
178
174
  resume: {
179
175
  type: "object",
180
176
  additionalProperties: false,
181
- required: ["sessionRef", "poolKey"],
177
+ required: ["sessionRef"],
182
178
  properties: {
183
179
  // ResumeAnchor.sessionRef = Record<string, string>(引擎定位键值对)
184
180
  sessionRef: { type: "object", additionalProperties: { type: "string" } },
185
- poolKey: { type: "string" },
186
181
  journalPath: { type: "string" },
187
182
  },
188
183
  },