agent-gear 2.8.0 → 2.10.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.
- package/AGENTS.md +35 -5
- package/dist/acp/bridge.d.ts +29 -1
- package/dist/acp/bridge.d.ts.map +1 -1
- package/dist/acp/bridge.js +171 -27
- package/dist/acp/bridge.js.map +1 -1
- package/dist/acp/bridge.test.js +198 -15
- package/dist/acp/bridge.test.js.map +1 -1
- package/dist/acp/detector.d.ts +2 -0
- package/dist/acp/detector.d.ts.map +1 -1
- package/dist/acp/detector.js +18 -3
- package/dist/acp/detector.js.map +1 -1
- package/dist/acp/detector.test.d.ts +2 -0
- package/dist/acp/detector.test.d.ts.map +1 -0
- package/dist/acp/detector.test.js +50 -0
- package/dist/acp/detector.test.js.map +1 -0
- package/dist/acp/initial-config.integration.test.d.ts +2 -0
- package/dist/acp/initial-config.integration.test.d.ts.map +1 -0
- package/dist/acp/initial-config.integration.test.js +95 -0
- package/dist/acp/initial-config.integration.test.js.map +1 -0
- package/dist/cloud-host/supervisor.d.ts +8 -19
- package/dist/cloud-host/supervisor.d.ts.map +1 -1
- package/dist/cloud-host/supervisor.js +7 -27
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +12 -6
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/commands/harness.d.ts +5 -1
- package/dist/commands/harness.d.ts.map +1 -1
- package/dist/commands/harness.js +7 -3
- package/dist/commands/harness.js.map +1 -1
- package/dist/exit-codes.d.ts +30 -0
- package/dist/exit-codes.d.ts.map +1 -0
- package/dist/exit-codes.js +52 -0
- package/dist/exit-codes.js.map +1 -0
- package/dist/exit-codes.test.d.ts +2 -0
- package/dist/exit-codes.test.d.ts.map +1 -0
- package/dist/exit-codes.test.js +61 -0
- package/dist/exit-codes.test.js.map +1 -0
- package/dist/harness/dsh-cli.d.ts +17 -0
- package/dist/harness/dsh-cli.d.ts.map +1 -0
- package/dist/harness/dsh-cli.js +51 -0
- package/dist/harness/dsh-cli.js.map +1 -0
- package/dist/harness/dsh-cli.test.d.ts +2 -0
- package/dist/harness/dsh-cli.test.d.ts.map +1 -0
- package/dist/harness/dsh-cli.test.js +51 -0
- package/dist/harness/dsh-cli.test.js.map +1 -0
- package/dist/harness/dsh-config.atomic-failure.test.d.ts +2 -0
- package/dist/harness/dsh-config.atomic-failure.test.d.ts.map +1 -0
- package/dist/harness/dsh-config.atomic-failure.test.js +35 -0
- package/dist/harness/dsh-config.atomic-failure.test.js.map +1 -0
- package/dist/harness/dsh-config.d.ts +46 -0
- package/dist/harness/dsh-config.d.ts.map +1 -0
- package/dist/harness/dsh-config.js +228 -0
- package/dist/harness/dsh-config.js.map +1 -0
- package/dist/harness/dsh-config.test.d.ts +2 -0
- package/dist/harness/dsh-config.test.d.ts.map +1 -0
- package/dist/harness/dsh-config.test.js +229 -0
- package/dist/harness/dsh-config.test.js.map +1 -0
- package/dist/harness/env.d.ts +11 -0
- package/dist/harness/env.d.ts.map +1 -1
- package/dist/harness/env.js +23 -0
- package/dist/harness/env.js.map +1 -1
- package/dist/harness/env.test.js +25 -3
- package/dist/harness/env.test.js.map +1 -1
- package/dist/harness/opencode-cli.d.ts +5 -0
- package/dist/harness/opencode-cli.d.ts.map +1 -1
- package/dist/harness/opencode-cli.js +8 -1
- package/dist/harness/opencode-cli.js.map +1 -1
- package/dist/harness/prepare.d.ts +27 -9
- package/dist/harness/prepare.d.ts.map +1 -1
- package/dist/harness/prepare.js +67 -23
- package/dist/harness/prepare.js.map +1 -1
- package/dist/harness/prepare.test.js +112 -5
- package/dist/harness/prepare.test.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/socketio-client.auth-gate.test.js +3 -3
- package/dist/socketio-client.auth-gate.test.js.map +1 -1
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.js +27 -3
- package/dist/socketio-client.js.map +1 -1
- package/dist/socketio-client.test.js +37 -0
- package/dist/socketio-client.test.js.map +1 -1
- package/dist/workload-executor.test.js +2 -2
- package/dist/workload-executor.test.js.map +1 -1
- package/package.json +3 -2
- package/src/acp/__fixtures__/fake-initial-config-agent.mjs +113 -0
- package/src/acp/bridge.test.ts +223 -15
- package/src/acp/bridge.ts +188 -27
- package/src/acp/detector.test.ts +64 -0
- package/src/acp/detector.ts +18 -3
- package/src/acp/initial-config.integration.test.ts +120 -0
- package/src/cloud-host/supervisor.test.ts +15 -6
- package/src/cloud-host/supervisor.ts +22 -35
- package/src/commands/harness.ts +9 -3
- package/src/exit-codes.test.ts +73 -0
- package/src/exit-codes.ts +59 -0
- package/src/harness/dsh-cli.test.ts +58 -0
- package/src/harness/dsh-cli.ts +67 -0
- package/src/harness/dsh-config.atomic-failure.test.ts +51 -0
- package/src/harness/dsh-config.test.ts +275 -0
- package/src/harness/dsh-config.ts +280 -0
- package/src/harness/env.test.ts +34 -3
- package/src/harness/env.ts +26 -0
- package/src/harness/opencode-cli.ts +9 -2
- package/src/harness/prepare.test.ts +131 -5
- package/src/harness/prepare.ts +103 -27
- package/src/index.ts +7 -0
- package/src/socketio-client.auth-gate.test.ts +3 -3
- package/src/socketio-client.test.ts +44 -0
- package/src/socketio-client.ts +27 -3
- package/src/workload-executor.test.ts +2 -2
- package/tests/acp-detector.test.ts +28 -8
- package/tests/commands/harness.test.ts +18 -8
- package/tests/socketio-client.test.ts +4 -4
|
@@ -11,12 +11,26 @@ import {
|
|
|
11
11
|
type GithubCredentialRefreshResult,
|
|
12
12
|
} from '../github/credential-manager.js';
|
|
13
13
|
import type { WorkloadRunPayload } from '../workload-executor.js';
|
|
14
|
+
import {
|
|
15
|
+
EXIT_SERVER_SHUTDOWN,
|
|
16
|
+
isFailureShutdownReason,
|
|
17
|
+
isServerTerminationReason,
|
|
18
|
+
resolveShutdownExitCode,
|
|
19
|
+
} from '../exit-codes.js';
|
|
20
|
+
|
|
21
|
+
// 既有导入路径(`cloud-host/supervisor.js`)继续可用;实现统一在 `exit-codes.ts`。
|
|
22
|
+
export {
|
|
23
|
+
EXIT_SERVER_SHUTDOWN,
|
|
24
|
+
isFailureShutdownReason,
|
|
25
|
+
isServerTerminationReason,
|
|
26
|
+
resolveShutdownExitCode,
|
|
27
|
+
} from '../exit-codes.js';
|
|
14
28
|
|
|
15
29
|
/** 未设置 `HARNESS_INIT_WAIT_MS` 时的初始化等待上限(10 分钟)。 */
|
|
16
30
|
export const DEFAULT_HARNESS_INIT_WAIT_MS = 600_000;
|
|
17
31
|
|
|
18
|
-
/** 未设置 `ZAG_DEFAULT_WORKSPACE`
|
|
19
|
-
export const DEFAULT_WORKSPACE = '/
|
|
32
|
+
/** 未设置 `ZAG_DEFAULT_WORKSPACE` 时的默认工作区(容器以 worker 用户运行)。 */
|
|
33
|
+
export const DEFAULT_WORKSPACE = '/home/worker/workspace';
|
|
20
34
|
|
|
21
35
|
/** 云电脑模式必需的环境变量(三者同时非空才进入 supervisor)。 */
|
|
22
36
|
export const CLOUD_HOST_MODE_KEYS = ['ZAG_HOST_ENDPOINT', 'ZAG_HOST_KEY', 'ZAG_CONTROLLER_KEY'] as const;
|
|
@@ -33,7 +47,7 @@ export interface CloudHostEnv {
|
|
|
33
47
|
serverEndpoint: string;
|
|
34
48
|
/** 注册上报的主机名(env `ZAG_HOST_NAME`,缺省 `os.hostname()`)。 */
|
|
35
49
|
hostName: string;
|
|
36
|
-
/** 默认工作区(env `ZAG_DEFAULT_WORKSPACE`,缺省 `/
|
|
50
|
+
/** 默认工作区(env `ZAG_DEFAULT_WORKSPACE`,缺省 `/home/worker/workspace`)。 */
|
|
37
51
|
workspace: string;
|
|
38
52
|
/** 初始化等待超时(env `HARNESS_INIT_WAIT_MS`,缺省 10 分钟)。 */
|
|
39
53
|
initWaitMs: number;
|
|
@@ -131,40 +145,13 @@ export interface CloudHostSupervisorHandle {
|
|
|
131
145
|
/**
|
|
132
146
|
* 关闭两端并退出(不发送 `unregister`)。
|
|
133
147
|
*
|
|
134
|
-
* `code` 缺省按 `reason`
|
|
135
|
-
*
|
|
136
|
-
*
|
|
148
|
+
* `code` 缺省按 `reason` 分类(见 `exit-codes.ts`):信号退出 → 0(入口保活重启);
|
|
149
|
+
* 服务端终止(非失败类 shutdown / `unregistered` / `INVALID_TOKEN`)→ 42(入口不重启);
|
|
150
|
+
* `version_mismatch:*` / 其它 `error:*` / 初始化失败类 → 1(入口退避自更新后重启,C-2)。
|
|
137
151
|
*/
|
|
138
152
|
shutdown: (reason: string, code?: number) => void;
|
|
139
153
|
}
|
|
140
154
|
|
|
141
|
-
/**
|
|
142
|
-
* 错误/失败类 shutdown reason 判定(C-2):
|
|
143
|
-
*
|
|
144
|
-
* - `socketio-client` 把 `VERSION_MISMATCH` / 其它 `error` 事件转成
|
|
145
|
-
* `version_mismatch:*` / `error:<code>` 前缀;
|
|
146
|
-
* - cloud-controller 的必须工作负载失败 reason 形如「必须工作负载「x」初始化失败」
|
|
147
|
-
* (经 `worker shutdown: ...` 包装后仍需识别);
|
|
148
|
-
* - 其余(服务端主动 shutdown、`unregistered`、SIGINT/SIGTERM)视为正常退出。
|
|
149
|
-
*/
|
|
150
|
-
export function isFailureShutdownReason(reason: string): boolean {
|
|
151
|
-
const normalized = reason.trim().toLowerCase();
|
|
152
|
-
// reason 可能被 `worker shutdown:` / `controller-api shutdown:` 包装,用 includes 判定前缀。
|
|
153
|
-
if (normalized.includes('version_mismatch:') || /(^|[\s:])error:/.test(normalized)) {
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
return (
|
|
157
|
-
reason.includes('初始化失败') ||
|
|
158
|
-
normalized.includes('init failed') ||
|
|
159
|
-
normalized.includes('initialization failed')
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** 由 shutdown reason 推导退出码:失败路径 1、正常退出 0。 */
|
|
164
|
-
export function resolveShutdownExitCode(reason: string): number {
|
|
165
|
-
return isFailureShutdownReason(reason) ? 1 : 0;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
155
|
/** 日志中的连接目标:仅 origin + namespace,绝不包含 query/token。 */
|
|
169
156
|
function describeEndpoint(endpoint: string): string {
|
|
170
157
|
const { origin, namespace } = parseEndpoint(endpoint);
|
|
@@ -178,7 +165,7 @@ function describeEndpoint(endpoint: string): string {
|
|
|
178
165
|
* 2. 先连 cloud-controller `/worker`(`ZAG_SERVER_ENDPOINT` + `ZAG_CONTROLLER_KEY`);
|
|
179
166
|
* 3. `registered.initializing=true` → 等待首个 `workload:run` 批次结束(成功/失败/超时)后
|
|
180
167
|
* 再连 controller-api(`ZAG_HOST_ENDPOINT` + `ZAG_HOST_KEY`);`false`/缺省立即连接;
|
|
181
|
-
* 4. 任一端 `shutdown` →
|
|
168
|
+
* 4. 任一端 `shutdown` → 关闭两端并退出(服务端终止 42 / 信号 0 / 失败 1);**始终不发送 `unregister`**;
|
|
182
169
|
* 5. 仅 controller-api 标准连接启动本地 MCP 回环服务(worker 连接跳过,避免端口冲突)。
|
|
183
170
|
*/
|
|
184
171
|
export async function startCloudHostSupervisor(
|
|
@@ -240,7 +227,7 @@ export async function startCloudHostSupervisor(
|
|
|
240
227
|
/* socket may already be closed */
|
|
241
228
|
}
|
|
242
229
|
// 不发送 unregister:容器销毁由 agent-api 编排(stopCloudHost / TTL / 失败回调)。
|
|
243
|
-
//
|
|
230
|
+
// 退出码三分见 exit-codes.ts:42 服务端终止 / 0 信号 / 1 失败(入口据此保活或收敛)。
|
|
244
231
|
exit(exitCode);
|
|
245
232
|
};
|
|
246
233
|
|
package/src/commands/harness.ts
CHANGED
|
@@ -10,9 +10,13 @@ export interface RegisterHarnessCommandsDeps {
|
|
|
10
10
|
* 注册 `agent-gear harness` 命令组。
|
|
11
11
|
*
|
|
12
12
|
* ```
|
|
13
|
-
* agent-gear harness prepare # 校验 env →
|
|
13
|
+
* agent-gear harness prepare # 按 ZAG_HARNESS_TYPES 校验 env → CLI 硬校验 → 注入配置
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
|
+
* 注入目标(按 `ZAG_HARNESS_TYPES` 分流;缺省/空 → `opencode`):
|
|
17
|
+
* - `opencode`:`opencode.jsonc` 的 `providers.zag`;
|
|
18
|
+
* - `deepseek_harness`:`profiles/acp/cordis.patch.yml` + `.credentials.yaml`。
|
|
19
|
+
*
|
|
16
20
|
* 失败以退出码 1 结束(供 harness 工作负载 `required=true` 触发容器强制注销);
|
|
17
21
|
* 输出/错误均对 `ZAG_MODEL_API_KEY` 等 secret 脱敏。
|
|
18
22
|
*/
|
|
@@ -22,11 +26,13 @@ export function registerHarnessCommands(
|
|
|
22
26
|
): Command {
|
|
23
27
|
const harness = program
|
|
24
28
|
.command('harness')
|
|
25
|
-
.description('Prepare harness runtime (OpenCode) for cloud-host containers');
|
|
29
|
+
.description('Prepare harness runtime (OpenCode / DeepSeek Harness) for cloud-host containers');
|
|
26
30
|
|
|
27
31
|
harness
|
|
28
32
|
.command('prepare')
|
|
29
|
-
.description(
|
|
33
|
+
.description(
|
|
34
|
+
'Validate harness env, verify CLI and write providers.zag into opencode.jsonc or profiles/acp for dsh',
|
|
35
|
+
)
|
|
30
36
|
.action(async () => {
|
|
31
37
|
try {
|
|
32
38
|
const result = await (deps.prepare ?? prepareHarness)();
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
EXIT_SERVER_SHUTDOWN,
|
|
4
|
+
isFailureShutdownReason,
|
|
5
|
+
isServerTerminationReason,
|
|
6
|
+
isSignalShutdownReason,
|
|
7
|
+
resolveShutdownExitCode,
|
|
8
|
+
} from './exit-codes.js';
|
|
9
|
+
|
|
10
|
+
describe('exit codes (contract §3.7)', () => {
|
|
11
|
+
it('T-EC1: EXIT_SERVER_SHUTDOWN is 42', () => {
|
|
12
|
+
expect(EXIT_SERVER_SHUTDOWN).toBe(42);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('T-EC2: signals exit 0 (keepalive restarts after accidental kill)', () => {
|
|
16
|
+
for (const reason of ['SIGINT', 'SIGTERM', 'signal: SIGTERM', ' worker shutdown: sigterm ']) {
|
|
17
|
+
expect(isSignalShutdownReason(reason)).toBe(true);
|
|
18
|
+
expect(resolveShutdownExitCode(reason)).toBe(0);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('T-EC3: server termination reasons exit 42', () => {
|
|
23
|
+
const reasons = [
|
|
24
|
+
'unregistered',
|
|
25
|
+
'worker shutdown: unregistered',
|
|
26
|
+
'controller-api shutdown: unregistered',
|
|
27
|
+
'error: INVALID_TOKEN',
|
|
28
|
+
'worker shutdown: error: INVALID_TOKEN',
|
|
29
|
+
'worker shutdown: error:invalid_token',
|
|
30
|
+
];
|
|
31
|
+
for (const reason of reasons) {
|
|
32
|
+
expect(isServerTerminationReason(reason)).toBe(true);
|
|
33
|
+
expect(resolveShutdownExitCode(reason)).toBe(42);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('T-EC4: failure reasons exit 1', () => {
|
|
38
|
+
const reasons = [
|
|
39
|
+
'version_mismatch: agent-gear 版本不兼容',
|
|
40
|
+
'worker shutdown: version_mismatch: too old',
|
|
41
|
+
'error: REGISTER_FAILED',
|
|
42
|
+
'controller-api shutdown: error: UNKNOWN',
|
|
43
|
+
'必须工作负载「OpenCode(Harness)」初始化失败',
|
|
44
|
+
'worker shutdown: 必须工作负载「x」初始化失败',
|
|
45
|
+
'required init failed',
|
|
46
|
+
'required initialization failed',
|
|
47
|
+
];
|
|
48
|
+
for (const reason of reasons) {
|
|
49
|
+
expect(isFailureShutdownReason(reason)).toBe(true);
|
|
50
|
+
expect(resolveShutdownExitCode(reason)).toBe(1);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('T-EC5: plain server shutdown falls through to 42', () => {
|
|
55
|
+
expect(resolveShutdownExitCode('server requested shutdown')).toBe(42);
|
|
56
|
+
expect(resolveShutdownExitCode('worker shutdown: 强制销毁')).toBe(42);
|
|
57
|
+
expect(resolveShutdownExitCode('controller-api shutdown: key_revoked')).toBe(42);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('T-EC6: INVALID_TOKEN overrides the generic error: failure prefix', () => {
|
|
61
|
+
expect(isFailureShutdownReason('error: INVALID_TOKEN')).toBe(true);
|
|
62
|
+
expect(resolveShutdownExitCode('error: INVALID_TOKEN')).toBe(42);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('contract anchor', () => {
|
|
67
|
+
it('T-EC7: contract documents exit code 42', async () => {
|
|
68
|
+
const { readFileSync } = await import('node:fs');
|
|
69
|
+
const contract = readFileSync(new URL('../../../contracts/agent-controller-websocket.md', import.meta.url), 'utf8');
|
|
70
|
+
expect(contract).toContain('`42`');
|
|
71
|
+
expect(contract).toContain('EXIT_SERVER_SHUTDOWN');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 进程退出码分类(契约 `agent-controller-websocket.md` §3.7)。
|
|
3
|
+
*
|
|
4
|
+
* 三分口径:
|
|
5
|
+
* - `42` 服务端终止(`unregistered` / 非失败类 `shutdown` / `error: INVALID_TOKEN`)→ 入口不重启;
|
|
6
|
+
* - `0` 自发 / 信号退出(`SIGINT` / `SIGTERM`)→ 入口保活重启(误杀恢复);
|
|
7
|
+
* - `1` 失败类(`version_mismatch:*` / 其它 `error:*` / 初始化失败)→ 入口退避自更新后重启。
|
|
8
|
+
*
|
|
9
|
+
* `socketio-client.ts` 与 `cloud-host/supervisor.ts` 共用本模块,避免两者循环依赖。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** 服务端终止退出码:容器入口视为正常结束、不重启。 */
|
|
13
|
+
export const EXIT_SERVER_SHUTDOWN = 42;
|
|
14
|
+
|
|
15
|
+
/** 信号类退出码:入口保活重启。 */
|
|
16
|
+
export const EXIT_SIGNAL_SHUTDOWN = 0;
|
|
17
|
+
|
|
18
|
+
/** 失败类退出码:入口退避自更新后重启。 */
|
|
19
|
+
export const EXIT_FAILURE_SHUTDOWN = 1;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 错误/失败类 reason 判定(自 supervisor 逐字迁移,语义不得变更)。
|
|
23
|
+
*/
|
|
24
|
+
export function isFailureShutdownReason(reason: string): boolean {
|
|
25
|
+
const normalized = reason.trim().toLowerCase();
|
|
26
|
+
if (normalized.includes('version_mismatch:') || /(^|[\s:])error:/.test(normalized)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return (
|
|
30
|
+
reason.includes('初始化失败') ||
|
|
31
|
+
normalized.includes('init failed') ||
|
|
32
|
+
normalized.includes('initialization failed')
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 信号终止 reason(允许 `signal: SIGTERM` 形态)。 */
|
|
37
|
+
export function isSignalShutdownReason(reason: string): boolean {
|
|
38
|
+
return /(^|[\s:])(sigint|sigterm)\s*$/i.test(reason.trim());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 服务端终止 reason(42):`unregistered` 或 `error: INVALID_TOKEN`(可带包装前缀)。 */
|
|
42
|
+
export function isServerTerminationReason(reason: string): boolean {
|
|
43
|
+
const normalized = reason.trim().toLowerCase();
|
|
44
|
+
return (
|
|
45
|
+
normalized.includes('unregistered') ||
|
|
46
|
+
/(^|[\s:])error:\s*invalid_token\b/.test(normalized)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 由 reason 推导退出码:信号 0 → 服务端终止 42 → 失败 1 → 其余 42。
|
|
52
|
+
* 判定顺序保证 `INVALID_TOKEN`(虽匹配 `error:`)归 42。
|
|
53
|
+
*/
|
|
54
|
+
export function resolveShutdownExitCode(reason: string): number {
|
|
55
|
+
if (isSignalShutdownReason(reason)) return EXIT_SIGNAL_SHUTDOWN;
|
|
56
|
+
if (isServerTerminationReason(reason)) return EXIT_SERVER_SHUTDOWN;
|
|
57
|
+
if (isFailureShutdownReason(reason)) return EXIT_FAILURE_SHUTDOWN;
|
|
58
|
+
return EXIT_SERVER_SHUTDOWN;
|
|
59
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { DshCliError, verifyDshCli } from './dsh-cli.js';
|
|
3
|
+
import type { CommandRunner } from './opencode-cli.js';
|
|
4
|
+
|
|
5
|
+
describe('verifyDshCli (T-CLI1)', () => {
|
|
6
|
+
it('runs `dsh --version` and returns the prerelease version unchanged', async () => {
|
|
7
|
+
const runner = vi.fn<CommandRunner>(async () => ({ stdout: '0.1.2-alpha.2\n', stderr: '' }));
|
|
8
|
+
|
|
9
|
+
await expect(verifyDshCli(runner)).resolves.toBe('0.1.2-alpha.2');
|
|
10
|
+
expect(runner).toHaveBeenCalledWith('dsh', ['--version']);
|
|
11
|
+
expect(runner).toHaveBeenCalledTimes(1);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('accepts the exact pinned version (v prefix / command prefix tolerated)', async () => {
|
|
15
|
+
const runner = vi.fn<CommandRunner>(async () => ({ stdout: 'v0.1.2-alpha.2\n', stderr: '' }));
|
|
16
|
+
await expect(verifyDshCli(runner, '0.1.2-alpha.2')).resolves.toBe('0.1.2-alpha.2');
|
|
17
|
+
|
|
18
|
+
const prefixed = vi.fn<CommandRunner>(async () => ({ stdout: 'dsh 0.1.2-alpha.2\n', stderr: '' }));
|
|
19
|
+
await expect(verifyDshCli(prefixed, '0.1.2-alpha.2')).resolves.toBe('dsh 0.1.2-alpha.2');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('fails on a version mismatch without leaking secrets', async () => {
|
|
23
|
+
const runner = vi.fn<CommandRunner>(async () => ({ stdout: '0.1.1\n', stderr: '' }));
|
|
24
|
+
let error: unknown;
|
|
25
|
+
try {
|
|
26
|
+
await verifyDshCli(runner, '0.1.2-alpha.2');
|
|
27
|
+
} catch (caught) {
|
|
28
|
+
error = caught;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
expect(error).toBeInstanceOf(DshCliError);
|
|
32
|
+
const message = (error as Error).message;
|
|
33
|
+
expect(message).toContain('version mismatch');
|
|
34
|
+
expect(message).toContain('0.1.2-alpha.2');
|
|
35
|
+
expect(message).toContain('0.1.1');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('fails when dsh prints no version output', async () => {
|
|
39
|
+
const runner = vi.fn<CommandRunner>(async () => ({ stdout: ' \n', stderr: '' }));
|
|
40
|
+
await expect(verifyDshCli(runner)).rejects.toBeInstanceOf(DshCliError);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('fails when the dsh command is missing', async () => {
|
|
44
|
+
const runner = vi.fn<CommandRunner>(async () => {
|
|
45
|
+
throw new Error('spawn dsh ENOENT');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
let error: unknown;
|
|
49
|
+
try {
|
|
50
|
+
await verifyDshCli(runner);
|
|
51
|
+
} catch (caught) {
|
|
52
|
+
error = caught;
|
|
53
|
+
}
|
|
54
|
+
expect(error).toBeInstanceOf(DshCliError);
|
|
55
|
+
expect((error as Error).message).toContain('dsh --version');
|
|
56
|
+
expect((error as Error).message).toContain('ENOENT');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { extractVersion, normalizeCliVersion, type CommandRunner } from './opencode-cli.js';
|
|
3
|
+
|
|
4
|
+
/** `dsh --version` 硬校验失败。 */
|
|
5
|
+
export class DshCliError extends Error {
|
|
6
|
+
constructor(message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'DshCliError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
13
|
+
|
|
14
|
+
/** 生产默认执行器:`dsh --version`(预发布段原样返回)。 */
|
|
15
|
+
const defaultRunner: CommandRunner = (command, args) =>
|
|
16
|
+
new Promise((resolve, reject) => {
|
|
17
|
+
execFile(
|
|
18
|
+
command,
|
|
19
|
+
args,
|
|
20
|
+
{ timeout: DEFAULT_TIMEOUT_MS, maxBuffer: 1024 * 1024, encoding: 'utf8' },
|
|
21
|
+
(error, stdout, stderr) => {
|
|
22
|
+
if (error) {
|
|
23
|
+
reject(error);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
resolve({ stdout: stdout ?? '', stderr: stderr ?? '' });
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `dsh --version` 硬校验(计划 §3 B2):
|
|
33
|
+
*
|
|
34
|
+
* - 版本归一化与 `verifyOpencodeCli` 完全一致(首个非空行 + 末段 + 去 `v` 前缀),
|
|
35
|
+
* `0.1.2-alpha.2` 预发布段原样比较(不引入 semver);
|
|
36
|
+
* - `expectedVersion` 非空时要求严格相等;init 脚本已固定双包版本,agent-gear 侧二次校验;
|
|
37
|
+
* - 只有 `--version`,不做 `-v` 回退(dsh 无该兼容需求)。
|
|
38
|
+
*
|
|
39
|
+
* 失败抛 {@link DshCliError},消息只包含命令与版本信息,不含任何 env/secret。
|
|
40
|
+
*/
|
|
41
|
+
export async function verifyDshCli(
|
|
42
|
+
runner: CommandRunner = defaultRunner,
|
|
43
|
+
expectedVersion?: string,
|
|
44
|
+
): Promise<string> {
|
|
45
|
+
const failures: string[] = [];
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const result = await runner('dsh', ['--version']);
|
|
49
|
+
const version = extractVersion(result.stdout, result.stderr);
|
|
50
|
+
if (version) {
|
|
51
|
+
if (expectedVersion && normalizeCliVersion(version) !== normalizeCliVersion(expectedVersion)) {
|
|
52
|
+
throw new DshCliError(
|
|
53
|
+
`DeepSeek Harness CLI version mismatch: expected ${expectedVersion}, got ${version}`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return version;
|
|
57
|
+
}
|
|
58
|
+
failures.push('`dsh --version` returned no version output');
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof DshCliError) throw error;
|
|
61
|
+
failures.push(`\`dsh --version\` failed: ${(error as Error).message}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
throw new DshCliError(
|
|
65
|
+
`DeepSeek Harness CLI version check failed; dsh is required by the harness workload (${failures.join('; ')})`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
const fs = vi.hoisted(() => ({
|
|
4
|
+
chmod: vi.fn(),
|
|
5
|
+
mkdir: vi.fn(),
|
|
6
|
+
readFile: vi.fn(),
|
|
7
|
+
rename: vi.fn(),
|
|
8
|
+
rm: vi.fn(),
|
|
9
|
+
stat: vi.fn(),
|
|
10
|
+
writeFile: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock('node:fs/promises', () => fs);
|
|
14
|
+
|
|
15
|
+
import { DshConfigError, writeDshConfig } from './dsh-config.js';
|
|
16
|
+
|
|
17
|
+
const ENV = {
|
|
18
|
+
modelBaseUrl: 'http://admin-api:8002/v1',
|
|
19
|
+
modelApiKey: 'zag_sk_super_secret',
|
|
20
|
+
modelIds: ['model-a'],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('T-DSH7 dsh config atomic write failure cleanup', () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
vi.clearAllMocks();
|
|
26
|
+
fs.mkdir.mockResolvedValue(undefined);
|
|
27
|
+
fs.chmod.mockResolvedValue(undefined);
|
|
28
|
+
fs.writeFile.mockResolvedValue(undefined);
|
|
29
|
+
fs.rm.mockResolvedValue(undefined);
|
|
30
|
+
// 目标文件不存在 → 走新建分支
|
|
31
|
+
fs.stat.mockRejectedValue(new Error('ENOENT'));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('removes the temp file when rename fails and preserves the original error surface', async () => {
|
|
35
|
+
fs.rename.mockRejectedValue(new Error('rename failed'));
|
|
36
|
+
|
|
37
|
+
await expect(writeDshConfig({ env: ENV, dshHome: '/tmp/opencode-dsh-atomic' })).rejects.toBeInstanceOf(
|
|
38
|
+
DshConfigError,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
expect(fs.writeFile).toHaveBeenCalledWith(
|
|
42
|
+
expect.stringMatching(/\/tmp\/opencode-dsh-atomic\/profiles\/acp\/\.cordis\.patch\..*\.tmp$/),
|
|
43
|
+
expect.any(String),
|
|
44
|
+
{ encoding: 'utf-8', mode: 0o600 },
|
|
45
|
+
);
|
|
46
|
+
expect(fs.rm).toHaveBeenCalledWith(
|
|
47
|
+
expect.stringMatching(/\/tmp\/opencode-dsh-atomic\/profiles\/acp\/\.cordis\.patch\..*\.tmp$/),
|
|
48
|
+
{ force: true },
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
});
|