lark-remote 0.4.1 → 0.4.3

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.
Files changed (86) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.en.md +2 -2
  3. package/README.md +2 -2
  4. package/dist/bridge/approval-coordinator.d.ts +2 -0
  5. package/dist/bridge/approval-coordinator.js +16 -7
  6. package/dist/bridge/index.d.ts +22 -0
  7. package/dist/bridge/index.js +138 -56
  8. package/dist/card/approval-render.js +9 -8
  9. package/dist/card/card-budget.js +39 -26
  10. package/dist/card/run-renderer.js +9 -2
  11. package/dist/card/text-truncate.d.ts +9 -10
  12. package/dist/card/text-truncate.js +48 -43
  13. package/dist/cli.js +17 -0
  14. package/dist/config/codex-config.d.ts +3 -0
  15. package/dist/config/codex-config.js +4 -1
  16. package/dist/config/dir.js +6 -6
  17. package/dist/config/index.d.ts +7 -5
  18. package/dist/config/index.js +52 -13
  19. package/dist/config/kimi-config.d.ts +4 -1
  20. package/dist/config/kimi-config.js +5 -2
  21. package/dist/config/pi-config.d.ts +2 -7
  22. package/dist/config/pi-config.js +6 -6
  23. package/dist/config/wizard.js +8 -4
  24. package/dist/connector/index.d.ts +19 -0
  25. package/dist/connector/index.js +134 -23
  26. package/dist/inbound/placeholder.d.ts +3 -2
  27. package/dist/inbound/placeholder.js +17 -7
  28. package/dist/index.js +28 -6
  29. package/dist/instance-lock.d.ts +31 -15
  30. package/dist/instance-lock.js +64 -62
  31. package/dist/platform/agent-stopper.d.ts +29 -7
  32. package/dist/platform/agent-stopper.js +40 -8
  33. package/dist/platform/identity.d.ts +59 -3
  34. package/dist/platform/identity.js +105 -30
  35. package/dist/platform/path.d.ts +0 -7
  36. package/dist/platform/path.js +1 -51
  37. package/dist/platform/shell.d.ts +14 -0
  38. package/dist/platform/shell.js +35 -2
  39. package/dist/platform/sleep-blocker.d.ts +6 -0
  40. package/dist/platform/sleep-blocker.js +31 -5
  41. package/dist/platform/spawn.d.ts +1 -1
  42. package/dist/platform/spawn.js +1 -1
  43. package/dist/platform/terminator-posix.d.ts +1 -29
  44. package/dist/platform/terminator-posix.js +32 -30
  45. package/dist/platform/terminator-win32.js +4 -2
  46. package/dist/platform/terminator.d.ts +2 -2
  47. package/dist/platform/terminator.js +17 -2
  48. package/dist/platform/types.d.ts +3 -0
  49. package/dist/router/card-action-feedback.d.ts +17 -0
  50. package/dist/router/card-action-feedback.js +31 -0
  51. package/dist/router/card-helpers.d.ts +38 -2
  52. package/dist/router/card-helpers.js +79 -2
  53. package/dist/router/config/claude.js +8 -5
  54. package/dist/router/index.d.ts +41 -6
  55. package/dist/router/index.js +254 -84
  56. package/dist/runner/claude/runner.js +1 -1
  57. package/dist/runner/claude/session.d.ts +2 -6
  58. package/dist/runner/claude/session.js +7 -8
  59. package/dist/runner/codex/app-server/protocol-types.d.ts +4 -0
  60. package/dist/runner/codex/app-server/protocol-types.js +4 -0
  61. package/dist/runner/codex/app-server/runner.js +3 -0
  62. package/dist/runner/common/acp/base-acp-runner.js +7 -0
  63. package/dist/runner/common/connection-based-runner.d.ts +18 -0
  64. package/dist/runner/common/connection-based-runner.js +31 -0
  65. package/dist/runner/common/jsonrpc/client.js +6 -1
  66. package/dist/runner/common/jsonrpc/connection-manager.d.ts +20 -0
  67. package/dist/runner/common/jsonrpc/connection-manager.js +27 -1
  68. package/dist/runner/common/jsonrpc/transport.d.ts +29 -1
  69. package/dist/runner/common/jsonrpc/transport.js +79 -10
  70. package/dist/runner/common/spawning-runner.d.ts +49 -19
  71. package/dist/runner/common/spawning-runner.js +94 -54
  72. package/dist/runner/dsh/client.d.ts +1 -3
  73. package/dist/runner/dsh/client.js +0 -4
  74. package/dist/runner/dsh/types.d.ts +0 -36
  75. package/dist/runner/kimi/acp/runner.d.ts +18 -1
  76. package/dist/runner/kimi/acp/runner.js +100 -42
  77. package/dist/runner/kimi/acp/translator.d.ts +0 -1
  78. package/dist/runner/opencode/acp/translator.d.ts +0 -1
  79. package/dist/runner/pi/rpc/protocol-types.d.ts +4 -0
  80. package/dist/runner/pi/rpc/runner.js +3 -0
  81. package/dist/runner/resolve-agent-choices.js +0 -5
  82. package/package.json +2 -2
  83. package/dist/runner/common/process-stopper.d.ts +0 -7
  84. package/dist/runner/common/process-stopper.js +0 -8
  85. package/dist/runner/dsh/fake-dsh-server.d.ts +0 -60
  86. package/dist/runner/dsh/fake-dsh-server.js +0 -156
@@ -25,7 +25,7 @@
25
25
  * 生命周期:一个 workspace 一个长驻进程(lifetime='workspace')。每次
26
26
  * run(message) = 写一条 user 消息 + 消费 stdout 事件直到本 turn 的 result;
27
27
  * 进程在 turn 之间保持存活(stdin 不关闭),/stop / /new / /cd / 看门狗超时
28
- * 时经 ProcessStopper 组杀,下条消息按 SessionStore 的 sessionId --resume。
28
+ * 时经 Terminator 组杀,下条消息按 SessionStore 的 sessionId --resume。
29
29
  */
30
30
  import { readFile } from 'node:fs/promises';
31
31
  import { silentlyUnlink } from '../../common/fs.js';
@@ -43,7 +43,7 @@ const DEFAULT_IDLE_TTL_MS = 30 * 60 * 1000;
43
43
  const COMPACTION_RESULT_SUBTYPES = new Set(['compact', 'compaction']);
44
44
  /**
45
45
  * ClaudeSession extends SpawningRunner 复用其 spawn/pid 文件/killOrphan/
46
- * ProcessStopper/SpawnHeartbeat/退出分发器机制(P1-1/P1-10/P1-11 契约保持),
46
+ * Terminator/SpawnHeartbeat/退出分发器机制(P1-1/P1-10/P1-11 契约保持),
47
47
  * 但 run() 覆盖为「长驻 + 按 turn 消费」:进程跨 turn 存活,turn 结束以
48
48
  * stream-json result 事件(非 compact)为界,而不是进程退出。
49
49
  */
@@ -97,6 +97,8 @@ export class ClaudeSession extends SpawningRunner {
97
97
  spawnHeartbeatMs: opts.spawnHeartbeatMs,
98
98
  pidFilePrefix: 'claude',
99
99
  logTag: 'claude-runner',
100
+ // win32 查协议停止通道用的 agent key(claude 的 channel = 控制通道/关 stdin)
101
+ agent: 'claude',
100
102
  });
101
103
  this.binary = 'claude';
102
104
  this.permissionMode = opts.permissionMode ?? 'bypassPermissions';
@@ -166,12 +168,6 @@ export class ClaudeSession extends SpawningRunner {
166
168
  this.armIdleTimer();
167
169
  }
168
170
  }
169
- /**
170
- * 写一条用户消息到 stdin(--input-format stream-json 的 user 事件)。
171
- */
172
- async sendUserMessage(message) {
173
- await this.writeUserMessage(message);
174
- }
175
171
  /**
176
172
  * 回写审批响应(允许/拒绝)。
177
173
  */
@@ -373,6 +369,9 @@ export class ClaudeSession extends SpawningRunner {
373
369
  if (generation === this.processGeneration) {
374
370
  this.streamEnded = true;
375
371
  this.currentProcess = null;
372
+ // 进程自行结束(崩溃/上游退出):注销协议停止通道,别把死 pid 的
373
+ // 条目留在注册表里等 pid 复用。
374
+ this.unregisterStopper();
376
375
  silentlyUnlink(this.pidFilePath);
377
376
  this.wakeWaiters();
378
377
  }
@@ -6,6 +6,10 @@
6
6
  * field names use the wire format exactly: camelCase paths for methods
7
7
  * (`item/agentMessage/delta`) and snake_case only where the server requires it
8
8
  * (e.g. permission profile fields).
9
+ *
10
+ * 本文件是上游 schema 的完整镜像,不是"用到什么抄什么":少数导出类型当前没有
11
+ * consumer,是有意保留(接新通知/新方法时不必回头重抄 schema)。静态扫描再报
12
+ * "零引用导出类型"指的就是这一批,别据此删除。
9
13
  */
10
14
  export type { JsonRpcRequest, JsonRpcNotification, JsonRpcSuccessResponse, JsonRpcErrorResponse, } from '../../common/jsonrpc/types.js';
11
15
  export interface ClientInfo {
@@ -6,6 +6,10 @@
6
6
  * field names use the wire format exactly: camelCase paths for methods
7
7
  * (`item/agentMessage/delta`) and snake_case only where the server requires it
8
8
  * (e.g. permission profile fields).
9
+ *
10
+ * 本文件是上游 schema 的完整镜像,不是"用到什么抄什么":少数导出类型当前没有
11
+ * consumer,是有意保留(接新通知/新方法时不必回头重抄 schema)。静态扫描再报
12
+ * "零引用导出类型"指的就是这一批,别据此删除。
9
13
  */
10
14
  // =============================================================================
11
15
  // Notifications (server → client)
@@ -136,6 +136,9 @@ export class CodexAppServerRunner extends ConnectionBasedRunner {
136
136
  },
137
137
  };
138
138
  this.connectionManager = new ConnectionManager(managerOpts);
139
+ // 协议停止通道(design §3.3):codex 的 cancel 是 `turn/interrupt` 请求,已由
140
+ // cancelCurrentTurn 实现;登记后 win32 优雅停止才有通道可用(否则一路树杀)。
141
+ this.connectionManager.stopper = ({ client }) => this.buildCooperativeStop(client);
139
142
  }
140
143
  get logTag() {
141
144
  return 'codex-app-server-runner';
@@ -15,6 +15,10 @@ export class BaseAcpRunner extends ConnectionBasedRunner {
15
15
  constructor(opts, connectionManager) {
16
16
  super(opts);
17
17
  this.connectionManager = connectionManager;
18
+ // 协议停止通道(design §3.3):ACP 的 cancel 就是 `session/cancel`,已由
19
+ // cancelCurrentTurn 实现。这里只做登记接线——manager 建在 super() 实参里,
20
+ // 那时 this 不可用,所以只能等 super() 返回后再赋值。
21
+ connectionManager.stopper = ({ client }) => this.buildCooperativeStop(client);
18
22
  }
19
23
  // =========================================================================
20
24
  // Shared plumbing
@@ -40,6 +44,9 @@ export class BaseAcpRunner extends ConnectionBasedRunner {
40
44
  this.promptSettled = false;
41
45
  this.promptSent = false;
42
46
  this.activeSessionId = null;
47
+ // 未答的审批随轮次一起作废:连接是按工作区长驻的,请求 id 会从 1 重新
48
+ // 计数,留着旧条目等于让下一轮的迟到的卡片点击回信到一条无关请求上。
49
+ this.pendingApprovals.clear();
43
50
  }
44
51
  async releaseConnection(cwd) {
45
52
  await this.connectionManager.release(cwd);
@@ -15,6 +15,7 @@
15
15
  * updateApprovalMode, getStatusInfo) and provide the small abstract hooks below.
16
16
  */
17
17
  import type { AgentKind, AgentSessionReader, AgentEvent, AgentRunner, AgentStatusInfo, SpawnOptions } from '../types.js';
18
+ import type { AgentStopper } from '../../platform/agent-stopper.js';
18
19
  export declare abstract class ConnectionBasedRunner<TClient, TEvent = AgentEvent> implements AgentRunner {
19
20
  readonly kind: AgentKind;
20
21
  readonly sessionReader: AgentSessionReader;
@@ -49,6 +50,23 @@ export declare abstract class ConnectionBasedRunner<TClient, TEvent = AgentEvent
49
50
  protected abstract cancelCurrentTurn(): Promise<void>;
50
51
  /** Set up the turn: acquire + thread|session new/resume + prompt. */
51
52
  protected abstract setupTurn(message: string, opts: SpawnOptions): Promise<void>;
53
+ /**
54
+ * 本连接的协议停止通道(design §3.3)。
55
+ *
56
+ * win32 上「优雅停止」没有可拦截的跨进程 SIGTERM,只能经 agent 自有协议通道
57
+ * 请求对方收摊。子类把这个方法的结果交给 ConnectionManager.stopper,由后者按
58
+ * `agent + pid` 登记到 AgentStopperRegistry,Terminator 停进程时按 pid 取回。
59
+ *
60
+ * 两件事,顺序不能反:
61
+ * 1. 若本连接就是当前在途 turn 的连接,先发协议取消(codex turn/interrupt /
62
+ * ACP session/cancel / pi abort)。**归属判定不能省**:runner 实例按 agent
63
+ * 维度共享(一个实例管多条 workspace 连接),无条件调用
64
+ * `cancelCurrentTurn()` 会用它自己的 currentClient/activeSessionId 去打断
65
+ * **另一个 workspace** 正在跑的 turn;
66
+ * 2. 关 stdin。stdio 型 agent server 在 stdin EOF 后自行退出——只发协议取消
67
+ * 不会让进程退出,优雅段会空转到 grace 超时后照样树杀,等于通道白注册。
68
+ */
69
+ protected buildCooperativeStop(client: TClient): AgentStopper;
52
70
  get isRunning(): boolean;
53
71
  /** Agent self-describing status info for /status display. */
54
72
  abstract getStatusInfo(): AgentStatusInfo;
@@ -27,6 +27,37 @@ export class ConnectionBasedRunner {
27
27
  this.sessionReader = opts.sessionReader;
28
28
  this.turnIdleTimeoutMs = opts.turnIdleTimeoutMs ?? defaultTurnIdleTimeoutMs;
29
29
  }
30
+ /**
31
+ * 本连接的协议停止通道(design §3.3)。
32
+ *
33
+ * win32 上「优雅停止」没有可拦截的跨进程 SIGTERM,只能经 agent 自有协议通道
34
+ * 请求对方收摊。子类把这个方法的结果交给 ConnectionManager.stopper,由后者按
35
+ * `agent + pid` 登记到 AgentStopperRegistry,Terminator 停进程时按 pid 取回。
36
+ *
37
+ * 两件事,顺序不能反:
38
+ * 1. 若本连接就是当前在途 turn 的连接,先发协议取消(codex turn/interrupt /
39
+ * ACP session/cancel / pi abort)。**归属判定不能省**:runner 实例按 agent
40
+ * 维度共享(一个实例管多条 workspace 连接),无条件调用
41
+ * `cancelCurrentTurn()` 会用它自己的 currentClient/activeSessionId 去打断
42
+ * **另一个 workspace** 正在跑的 turn;
43
+ * 2. 关 stdin。stdio 型 agent server 在 stdin EOF 后自行退出——只发协议取消
44
+ * 不会让进程退出,优雅段会空转到 grace 超时后照样树杀,等于通道白注册。
45
+ */
46
+ buildCooperativeStop(client) {
47
+ return async (proc) => {
48
+ if (this.currentClient === client) {
49
+ try {
50
+ await this.cancelCurrentTurn();
51
+ }
52
+ catch (err) {
53
+ getLogger().warn(`[${this.logTag}] cooperative stop: cancel failed: ${err.message}`);
54
+ }
55
+ }
56
+ if (proc.stdin && !proc.stdin.destroyed) {
57
+ proc.stdin.end();
58
+ }
59
+ };
60
+ }
30
61
  get isRunning() {
31
62
  return this._isRunning;
32
63
  }
@@ -128,7 +128,12 @@ export class JsonRpcClient {
128
128
  return new Promise((resolve, reject) => {
129
129
  // Clamp to setTimeout's max: callers may pass Infinity for turn-scoped
130
130
  // requests (session/prompt) — Infinity would fire immediately in Node.
131
- const effectiveTimeout = Math.min(timeoutMs ?? this.requestTimeoutMs, 2_147_483_647);
131
+ // 配置里的 `requestTimeoutMs: 0` = 不超时(同族 `turnIdleTimeoutMinutes`、
132
+ // `claude.idleTtlMinutes` 都是「0 = 禁用」)。按 0 直接 setTimeout 会让
133
+ // 每个请求在响应到达前抢先 reject,等于整个 agent 不可用。
134
+ const budget = timeoutMs ??
135
+ (this.requestTimeoutMs === 0 ? Number.POSITIVE_INFINITY : this.requestTimeoutMs);
136
+ const effectiveTimeout = Math.min(budget, 2_147_483_647);
132
137
  const timer = setTimeout(() => {
133
138
  this.pendingRequests.delete(id);
134
139
  reject(new RpcTimeoutError(`request timed out: ${method}`));
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import { JsonlRpcTransport } from './transport.js';
17
17
  import { JsonRpcClient } from './client.js';
18
+ import { AgentStopperRegistry, type AgentStopper } from '../../../platform/agent-stopper.js';
18
19
  /** Minimal client surface the manager needs from any protocol client. */
19
20
  export interface ConnectionClient {
20
21
  ready: boolean;
@@ -75,6 +76,8 @@ export interface ConnectionManagerOptions<TClient extends ConnectionClient = Jso
75
76
  }) => boolean;
76
77
  /** Log tag prefix for operational log lines. */
77
78
  logTag?: string;
79
+ /** 协议停止通道注册表注入(测试隔离);默认进程级单例,透传给 transport。 */
80
+ stoppers?: AgentStopperRegistry;
78
81
  }
79
82
  export declare class ConnectionManager<TClient extends ConnectionClient = JsonRpcClient> {
80
83
  private slots;
@@ -87,8 +90,22 @@ export declare class ConnectionManager<TClient extends ConnectionClient = JsonRp
87
90
  private readonly clientFactory;
88
91
  private readonly shouldReuse;
89
92
  private readonly logTag;
93
+ private readonly stoppers;
90
94
  /** Callback when a connection is lost — cleared from slot map. */
91
95
  onConnectionLost?: (workspace: string) => void;
96
+ /**
97
+ * 协议停止通道工厂(design §3.3):连接建立后按 pid 登记到 AgentStopperRegistry,
98
+ * 供 Terminator 在**优雅**停止时回调(win32 上没有可拦截的 SIGTERM,只能靠
99
+ * 协议通道让 agent 自己收摊)。返回 undefined = 该协议无通道。
100
+ *
101
+ * 做成「构造后赋值的字段」而不是构造选项:kimi/opencode 的 manager 建在
102
+ * `super(...)` 的实参里,那时 `this` 尚不可用,runner 无法把
103
+ * cancelCurrentTurn 塞进选项——只能等 super() 返回后赋值。
104
+ */
105
+ stopper?: (ctx: {
106
+ pid: number;
107
+ client: TClient;
108
+ }) => AgentStopper | undefined;
92
109
  constructor(opts: ConnectionManagerOptions<TClient>);
93
110
  /**
94
111
  * Acquire a connection for the given workspace.
@@ -117,6 +134,9 @@ export declare class ConnectionManager<TClient extends ConnectionClient = JsonRp
117
134
  notifyActivity(workspace: string): void;
118
135
  /**
119
136
  * Notify that the workspace is idle — arm the idle timer.
137
+ *
138
+ * `idleTtlMs: 0` = 不回收(与 claude 会话层的 `armIdleTimer` 同口径;按 0
139
+ * setTimeout 会秒删刚建好的连接,等于每条消息重起一次进程)。
120
140
  */
121
141
  notifyIdle(workspace: string): void;
122
142
  private createClient;
@@ -16,6 +16,7 @@
16
16
  import { JsonlRpcTransport } from './transport.js';
17
17
  import { JsonRpcClient } from './client.js';
18
18
  import { getLogger } from '../../../logger/index.js';
19
+ import { agentStopperRegistry, } from '../../../platform/agent-stopper.js';
19
20
  const DEFAULT_IDLE_TTL_MS = 30 * 60 * 1000; // 30 minutes
20
21
  const DEFAULT_REQUEST_TIMEOUT_MS = 60_000; // 60 seconds
21
22
  export class ConnectionManager {
@@ -29,8 +30,19 @@ export class ConnectionManager {
29
30
  clientFactory;
30
31
  shouldReuse;
31
32
  logTag;
33
+ stoppers;
32
34
  /** Callback when a connection is lost — cleared from slot map. */
33
35
  onConnectionLost;
36
+ /**
37
+ * 协议停止通道工厂(design §3.3):连接建立后按 pid 登记到 AgentStopperRegistry,
38
+ * 供 Terminator 在**优雅**停止时回调(win32 上没有可拦截的 SIGTERM,只能靠
39
+ * 协议通道让 agent 自己收摊)。返回 undefined = 该协议无通道。
40
+ *
41
+ * 做成「构造后赋值的字段」而不是构造选项:kimi/opencode 的 manager 建在
42
+ * `super(...)` 的实参里,那时 `this` 尚不可用,runner 无法把
43
+ * cancelCurrentTurn 塞进选项——只能等 super() 返回后赋值。
44
+ */
45
+ stopper;
34
46
  constructor(opts) {
35
47
  this.binary = opts.binary;
36
48
  this.buildArgs = opts.buildArgs ?? (() => opts.args ?? []);
@@ -47,6 +59,7 @@ export class ConnectionManager {
47
59
  }, ctx.requestTimeoutMs, this.initializeParams));
48
60
  this.shouldReuse = opts.shouldReuse ?? (({ client }) => client.ready && client.healthy);
49
61
  this.logTag = opts.logTag ?? 'jsonrpc-connection-manager';
62
+ this.stoppers = opts.stoppers ?? agentStopperRegistry;
50
63
  }
51
64
  /**
52
65
  * Acquire a connection for the given workspace.
@@ -142,8 +155,13 @@ export class ConnectionManager {
142
155
  }
143
156
  /**
144
157
  * Notify that the workspace is idle — arm the idle timer.
158
+ *
159
+ * `idleTtlMs: 0` = 不回收(与 claude 会话层的 `armIdleTimer` 同口径;按 0
160
+ * setTimeout 会秒删刚建好的连接,等于每条消息重起一次进程)。
145
161
  */
146
162
  notifyIdle(workspace) {
163
+ if (this.idleTtlMs <= 0)
164
+ return;
147
165
  const slot = this.slots.get(workspace);
148
166
  if (slot) {
149
167
  this.clearIdleTimer(slot);
@@ -157,13 +175,21 @@ export class ConnectionManager {
157
175
  // Internal
158
176
  // =========================================================================
159
177
  async createClient(workspace, req) {
178
+ let client = null;
160
179
  const transport = new JsonlRpcTransport({
161
180
  binary: this.binary,
162
181
  args: this.buildArgs(req),
163
182
  cwd: workspace,
164
183
  env: this.env,
184
+ stoppers: this.stoppers,
185
+ // 通道工厂要拿到**连接本身**:协议取消(codex turn/interrupt、ACP
186
+ // session/cancel)必须发在这条连接上,不能借 runner 的共享状态——那会
187
+ // 打断另一 workspace 正在跑的 turn。工厂是在 transport.start() 内侧调用
188
+ // 的,那时 client 已由下面的 clientFactory 赋好。
189
+ stopper: this.stopper
190
+ ? (pid) => this.stopper?.({ pid, client: client })
191
+ : undefined,
165
192
  });
166
- let client = null;
167
193
  client = this.clientFactory({
168
194
  transport,
169
195
  requestTimeoutMs: this.requestTimeoutMs,
@@ -6,6 +6,8 @@
6
6
  * each line on stdout is a JSON message from the server; each `write()`
7
7
  * sends a JSON line to stdin.
8
8
  */
9
+ import { type Terminator } from '../../../platform/terminator.js';
10
+ import { AgentStopperRegistry, type AgentStopper } from '../../../platform/agent-stopper.js';
9
11
  export interface TransportEvents {
10
12
  onMessage(msg: unknown): void;
11
13
  onClose(reason: string): void;
@@ -14,11 +16,17 @@ export declare class JsonlRpcTransport {
14
16
  private proc;
15
17
  private _closed;
16
18
  private events;
17
- private stopper;
19
+ private terminator;
18
20
  private readonly binary;
19
21
  private readonly args;
20
22
  private readonly cwd;
21
23
  private readonly env;
24
+ /** 协议停止通道查找键(与 Terminator 用的 agent key 同一来源,见构造函数)。 */
25
+ private readonly agent;
26
+ private readonly stoppers;
27
+ private readonly stopperFactory?;
28
+ /** 已登记的通道(agent+pid 键);null = 本连接没有可用通道或已注销。 */
29
+ private registration;
22
30
  /** JSON lines buffered while stdin is under backpressure (bounded by callers). */
23
31
  private writeQueue;
24
32
  private flushing;
@@ -29,7 +37,20 @@ export declare class JsonlRpcTransport {
29
37
  args: string[];
30
38
  cwd: string;
31
39
  env?: Record<string, string | undefined>;
40
+ /** win32 查协议停止通道用的 agent key;缺省取 binary 名('kimi'/'codex'/…) */
41
+ agent?: string;
42
+ /** 终止器注入(测试用);默认按平台建实现 */
43
+ terminator?: Terminator;
44
+ /**
45
+ * 协议停止通道工厂(design §3.3):spawn 成功后按 pid 建通道并登记到
46
+ * AgentStopperRegistry,进程退出/关闭时注销。返回 undefined = 无通道。
47
+ */
48
+ stopper?: (pid: number) => AgentStopper | undefined;
49
+ /** 协议通道注册表注入(测试隔离);默认进程级单例 */
50
+ stoppers?: AgentStopperRegistry;
32
51
  });
52
+ /** 子进程 pid(spawn 前/退出后为 undefined);供测试与连接层按 pid 定位。 */
53
+ get pid(): number | undefined;
33
54
  get closed(): boolean;
34
55
  /**
35
56
  * Spawn the child process and start reading its stdout.
@@ -48,5 +69,12 @@ export declare class JsonlRpcTransport {
48
69
  * Idempotent — safe to call multiple times.
49
70
  */
50
71
  close(): Promise<void>;
72
+ /**
73
+ * 按 pid 登记协议停止通道(工厂返回 undefined = 该 agent 无通道)。
74
+ * pid 是归属键:同一 agent 的多条长驻连接各有自己的通道,不能互相顶替。
75
+ */
76
+ private registerStopper;
77
+ /** 注销自己登记的那条通道(幂等;带身份校验,防误删同 pid 上的新通道)。 */
78
+ private unregisterStopper;
51
79
  private handleClose;
52
80
  }
@@ -6,8 +6,11 @@
6
6
  * each line on stdout is a JSON message from the server; each `write()`
7
7
  * sends a JSON line to stdin.
8
8
  */
9
+ import { StringDecoder } from 'node:string_decoder';
9
10
  import { spawnProcess, mergeProcessEnv, isWindowsCommandNotFoundLine, useDetachedProcessGroup, } from '../../../platform/spawn.js';
10
- import { ProcessStopper } from '../process-stopper.js';
11
+ import { binaryName } from '../../../platform/identity.js';
12
+ import { createTerminator } from '../../../platform/terminator.js';
13
+ import { agentStopperRegistry, } from '../../../platform/agent-stopper.js';
11
14
  import { getLogger } from '../../../logger/index.js';
12
15
  /** Max bytes for a single line before we disconnect (10MB). */
13
16
  const MAX_LINE_BYTES = 10 * 1024 * 1024;
@@ -17,11 +20,17 @@ export class JsonlRpcTransport {
17
20
  proc = null;
18
21
  _closed = false;
19
22
  events = null;
20
- stopper;
23
+ terminator;
21
24
  binary;
22
25
  args;
23
26
  cwd;
24
27
  env;
28
+ /** 协议停止通道查找键(与 Terminator 用的 agent key 同一来源,见构造函数)。 */
29
+ agent;
30
+ stoppers;
31
+ stopperFactory;
32
+ /** 已登记的通道(agent+pid 键);null = 本连接没有可用通道或已注销。 */
33
+ registration = null;
25
34
  /** JSON lines buffered while stdin is under backpressure (bounded by callers). */
26
35
  writeQueue = [];
27
36
  flushing = false;
@@ -32,7 +41,21 @@ export class JsonlRpcTransport {
32
41
  this.args = opts.args;
33
42
  this.cwd = opts.cwd;
34
43
  this.env = opts.env ?? {};
35
- this.stopper = new ProcessStopper({ graceMs: 5000 });
44
+ // 协议停止通道按 agent key 索引:调用方(connection-manager)只给 binary,
45
+ // 这里用 binaryName 归一(含 win32 的 `.cmd` 垫片与全路径两种形态),
46
+ // 免得每个协议 runner 都要重复传一遍 agent 名。
47
+ this.agent = opts.agent ?? binaryName(opts.binary);
48
+ this.stoppers = opts.stoppers ?? agentStopperRegistry;
49
+ this.stopperFactory = opts.stopper;
50
+ // Terminator 与注册表共用同一个 agent key 和同一张表:两边任一取错来源,
51
+ // 查表就静默失配(优雅段退化回树杀还不报错),所以都从这里下传。
52
+ this.terminator =
53
+ opts.terminator ??
54
+ createTerminator({ graceMs: 5000, agent: this.agent, stoppers: this.stoppers });
55
+ }
56
+ /** 子进程 pid(spawn 前/退出后为 undefined);供测试与连接层按 pid 定位。 */
57
+ get pid() {
58
+ return this.proc?.pid;
36
59
  }
37
60
  get closed() {
38
61
  return this._closed;
@@ -70,6 +93,9 @@ export class JsonlRpcTransport {
70
93
  return;
71
94
  }
72
95
  getLogger().info(`[jsonrpc-transport] spawned pid=${proc.pid} binary=${this.binary}`);
96
+ // 子进程起来后立刻登记协议停止通道:win32 上 graceful stop 没有信号可拦,
97
+ // 只有这条通道能让 agent server 自己收摊(关 stdin / 取消在途 turn)。
98
+ this.registerStopper(proc.pid);
73
99
  proc.stdin?.on('error', (err) => {
74
100
  getLogger().warn(`[jsonrpc-transport] stdin error: ${err.message}`);
75
101
  this.handleClose('epipe');
@@ -90,34 +116,50 @@ export class JsonlRpcTransport {
90
116
  }
91
117
  });
92
118
  // Stdout: line-split with 10MB line limit
119
+ // decoder 跨 chunk 缓冲半个多字节字符:逐 chunk toString 会把中文/emoji 打成
120
+ // 不可逆的 U+FFFD,且 remainder 拼接后无法复原
121
+ const decoder = new StringDecoder('utf8');
93
122
  let remainder = '';
94
123
  proc.stdout?.on('data', (chunk) => {
95
- remainder += chunk.toString('utf-8');
124
+ remainder += decoder.write(chunk);
96
125
  const lines = remainder.split('\n');
97
126
  // Lines except the last (incomplete) one
98
127
  remainder = lines.pop() ?? '';
99
128
  for (const line of lines) {
100
129
  if (line.length === 0)
101
130
  continue;
102
- if (line.length > MAX_LINE_BYTES) {
131
+ // 上限量的是 UTF-8 字节:中文 1 单元 = 3 字节,用 line.length 会低估 2-3×
132
+ if (Buffer.byteLength(line, 'utf8') > MAX_LINE_BYTES) {
103
133
  getLogger().error(`[jsonrpc-transport] line exceeds ${MAX_LINE_BYTES} bytes, disconnecting`);
104
134
  this.handleClose('parse_error');
105
135
  return;
106
136
  }
137
+ let msg;
107
138
  try {
108
- const msg = JSON.parse(line);
109
- events.onMessage(msg);
139
+ msg = JSON.parse(line);
110
140
  }
111
141
  catch (err) {
112
142
  getLogger().warn(`[jsonrpc-transport] failed to parse JSON: ${err.message} line=${line.slice(0, 200)}`);
143
+ continue;
144
+ }
145
+ // handler 抛错是消费方故障,与协议解码无关;单独捕获以免掩盖真实故障点,
146
+ // 且不能打断同一 chunk 里后续的帧
147
+ try {
148
+ events.onMessage(msg);
149
+ }
150
+ catch (err) {
151
+ getLogger().error(`[jsonrpc-transport] onMessage handler threw: ${err instanceof Error ? err.message : String(err)}`);
113
152
  }
114
153
  }
115
154
  });
116
155
  // Handle process exit
117
156
  const onExit = (code, signal) => {
118
157
  this._closed = true;
158
+ // 进程已经没了:留着旧 pid 的通道只会在 pid 复用后指向无关进程。
159
+ this.unregisterStopper();
119
160
  this.proc = null;
120
- // Flush remaining buffer
161
+ // Flush remaining buffer(decoder.end() 吐出最后半个多字节字符)
162
+ remainder += decoder.end();
121
163
  if (remainder.length > 0) {
122
164
  try {
123
165
  const msg = JSON.parse(remainder);
@@ -209,7 +251,7 @@ export class JsonlRpcTransport {
209
251
  const proc = this.proc;
210
252
  if (proc) {
211
253
  try {
212
- await this.stopper.stop(proc);
254
+ await this.terminator.stop(proc, { immediate: false });
213
255
  }
214
256
  catch {
215
257
  // Ignore stop errors — process may already be dead
@@ -218,6 +260,27 @@ export class JsonlRpcTransport {
218
260
  proc.stdin.end();
219
261
  }
220
262
  }
263
+ // 注销必须在 stop() 之后:Terminator 就是在上面那次调用里查表取通道的。
264
+ this.unregisterStopper();
265
+ }
266
+ /**
267
+ * 按 pid 登记协议停止通道(工厂返回 undefined = 该 agent 无通道)。
268
+ * pid 是归属键:同一 agent 的多条长驻连接各有自己的通道,不能互相顶替。
269
+ */
270
+ registerStopper(pid) {
271
+ const stopper = this.stopperFactory?.(pid);
272
+ if (!stopper)
273
+ return;
274
+ this.stoppers.register(this.agent, pid, stopper);
275
+ this.registration = { pid, stopper };
276
+ }
277
+ /** 注销自己登记的那条通道(幂等;带身份校验,防误删同 pid 上的新通道)。 */
278
+ unregisterStopper() {
279
+ const reg = this.registration;
280
+ if (!reg)
281
+ return;
282
+ this.registration = null;
283
+ this.stoppers.unregister(this.agent, reg.pid, reg.stopper);
221
284
  }
222
285
  handleClose(reason) {
223
286
  if (this._closed)
@@ -237,8 +300,14 @@ export class JsonlRpcTransport {
237
300
  }
238
301
  // 异常路径(EPIPE/超长行/进程 error)也要收掉子进程,避免孤儿进程
239
302
  // 继续运行(如超长行场景子进程还在往 stdout 灌数据)。
240
- void this.stopper.stop(proc).catch((err) => {
303
+ void this.terminator
304
+ .stop(proc, { immediate: false })
305
+ .catch((err) => {
241
306
  getLogger().warn(`[jsonrpc-transport] failed to stop process during close: ${err instanceof Error ? err.message : String(err)}`);
307
+ })
308
+ .finally(() => {
309
+ // 通道查表发生在上面这次 stop() 里,注销必须等它落定。
310
+ this.unregisterStopper();
242
311
  });
243
312
  }
244
313
  // 统一关闭出口:EPIPE/超长行/进程 error 都必须通知上层(connection-manager