agent-gear 2.7.1 → 2.9.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 +22 -1
- package/dist/acp/auth-gate.d.ts +62 -0
- package/dist/acp/auth-gate.d.ts.map +1 -0
- package/dist/acp/auth-gate.js +110 -0
- package/dist/acp/auth-gate.js.map +1 -0
- package/dist/acp/auth-gate.test.d.ts +2 -0
- package/dist/acp/auth-gate.test.d.ts.map +1 -0
- package/dist/acp/auth-gate.test.js +126 -0
- package/dist/acp/auth-gate.test.js.map +1 -0
- 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/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/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 +23 -1
- package/dist/harness/env.test.js.map +1 -1
- package/dist/harness/kimi-login.d.ts +124 -0
- package/dist/harness/kimi-login.d.ts.map +1 -0
- package/dist/harness/kimi-login.js +263 -0
- package/dist/harness/kimi-login.js.map +1 -0
- package/dist/harness/kimi-login.test.d.ts +2 -0
- package/dist/harness/kimi-login.test.d.ts.map +1 -0
- package/dist/harness/kimi-login.test.js +193 -0
- package/dist/harness/kimi-login.test.js.map +1 -0
- 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 +110 -3
- package/dist/harness/prepare.test.js.map +1 -1
- package/dist/socketio-client.auth-gate.test.d.ts +2 -0
- package/dist/socketio-client.auth-gate.test.d.ts.map +1 -0
- package/dist/socketio-client.auth-gate.test.js +179 -0
- package/dist/socketio-client.auth-gate.test.js.map +1 -0
- package/dist/socketio-client.d.ts +7 -0
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.js +32 -2
- package/dist/socketio-client.js.map +1 -1
- package/package.json +3 -2
- package/src/acp/auth-gate.test.ts +145 -0
- package/src/acp/auth-gate.ts +144 -0
- package/src/acp/detector.test.ts +64 -0
- package/src/acp/detector.ts +18 -3
- package/src/commands/harness.ts +9 -3
- 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 +32 -1
- package/src/harness/env.ts +26 -0
- package/src/harness/kimi-login.test.ts +222 -0
- package/src/harness/kimi-login.ts +328 -0
- package/src/harness/opencode-cli.ts +9 -2
- package/src/harness/prepare.test.ts +129 -3
- package/src/harness/prepare.ts +103 -27
- package/src/socketio-client.auth-gate.test.ts +200 -0
- package/src/socketio-client.ts +36 -2
- package/tests/acp-detector.test.ts +28 -8
- package/tests/commands/harness.test.ts +17 -7
- package/tests/socketio-client.test.ts +2 -2
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
const childProcess = vi.hoisted(() => ({ exec: vi.fn() }));
|
|
4
|
+
|
|
5
|
+
vi.mock('child_process', () => childProcess);
|
|
6
|
+
|
|
7
|
+
import { AcpDetector } from './detector.js';
|
|
8
|
+
|
|
9
|
+
type ExecCallback = (error: Error | null, result?: { stdout: string; stderr: string }) => void;
|
|
10
|
+
|
|
11
|
+
function mockExec(handler: (command: string, callback: ExecCallback) => void): void {
|
|
12
|
+
childProcess.exec.mockImplementation(handler);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function mockOnlyDsh(stdout: string): void {
|
|
16
|
+
mockExec((command: string, callback: ExecCallback) => {
|
|
17
|
+
if (command === 'dsh --version') {
|
|
18
|
+
callback(null, { stdout, stderr: '' });
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
callback(new Error('not available'));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('AcpDetector deepseek_harness (T-DET*)', () => {
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
vi.clearAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('T-DET1: dsh --version 成功时 deepseek_harness 可用', async () => {
|
|
31
|
+
mockOnlyDsh('0.1.2-alpha.2\n');
|
|
32
|
+
|
|
33
|
+
const result = await new AcpDetector().detect();
|
|
34
|
+
expect(result.find((item) => item.type === 'deepseek_harness')).toEqual({
|
|
35
|
+
type: 'deepseek_harness',
|
|
36
|
+
available: true,
|
|
37
|
+
version: '0.1.2-alpha.2',
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('T-DET1b: 缺命令时 deepseek_harness 不可用(不抛错)', async () => {
|
|
42
|
+
mockExec((_command: string, callback: ExecCallback) => callback(new Error('spawn dsh ENOENT')));
|
|
43
|
+
|
|
44
|
+
const result = await new AcpDetector().detect();
|
|
45
|
+
expect(result.find((item) => item.type === 'deepseek_harness')).toEqual({
|
|
46
|
+
type: 'deepseek_harness',
|
|
47
|
+
available: false,
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('T-DET2: 预发布段保留', async () => {
|
|
52
|
+
mockOnlyDsh('0.1.2-alpha.2\n');
|
|
53
|
+
|
|
54
|
+
const result = await new AcpDetector().detect();
|
|
55
|
+
expect(result.find((item) => item.type === 'deepseek_harness')?.version).toBe('0.1.2-alpha.2');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('T-DET3: spawner 为 dsh --profile acp', () => {
|
|
59
|
+
expect(new AcpDetector().getSpawnerConfig('deepseek_harness')).toEqual({
|
|
60
|
+
command: 'dsh',
|
|
61
|
+
args: ['--profile', 'acp'],
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
package/src/acp/detector.ts
CHANGED
|
@@ -11,7 +11,7 @@ interface AcpServerInfo {
|
|
|
11
11
|
|
|
12
12
|
export class AcpDetector {
|
|
13
13
|
async detect(): Promise<AcpServerInfo[]> {
|
|
14
|
-
const [opencode, copilot, claudeCode, geminiCli, codex, mimoCode, kimiCode] = await Promise.all([
|
|
14
|
+
const [opencode, copilot, claudeCode, geminiCli, codex, mimoCode, kimiCode, deepseekHarness] = await Promise.all([
|
|
15
15
|
this.detectOpenCode(),
|
|
16
16
|
this.detectGitHubCopilot(),
|
|
17
17
|
this.detectClaudeCode(),
|
|
@@ -19,8 +19,9 @@ export class AcpDetector {
|
|
|
19
19
|
this.detectCodex(),
|
|
20
20
|
this.detectMiMoCode(),
|
|
21
21
|
this.detectKimiCode(),
|
|
22
|
+
this.detectDeepseekHarness(),
|
|
22
23
|
]);
|
|
23
|
-
return [opencode, copilot, claudeCode, geminiCli, codex, mimoCode, kimiCode];
|
|
24
|
+
return [opencode, copilot, claudeCode, geminiCli, codex, mimoCode, kimiCode, deepseekHarness];
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
private async detectOpenCode(): Promise<AcpServerInfo> {
|
|
@@ -93,6 +94,17 @@ export class AcpDetector {
|
|
|
93
94
|
}
|
|
94
95
|
}
|
|
95
96
|
|
|
97
|
+
/** DeepSeek Harness:`dsh --version` 输出纯版本号(预发布段如 `0.1.2-alpha.2` 保留)。 */
|
|
98
|
+
private async detectDeepseekHarness(): Promise<AcpServerInfo> {
|
|
99
|
+
try {
|
|
100
|
+
const { stdout } = await execAsync('dsh --version');
|
|
101
|
+
const version = this.parseVersion(stdout);
|
|
102
|
+
return { type: 'deepseek_harness', available: true, version };
|
|
103
|
+
} catch {
|
|
104
|
+
return { type: 'deepseek_harness', available: false };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
96
108
|
getSpawnerConfig(serverType: string): { command: string; args: string[] } {
|
|
97
109
|
switch (serverType) {
|
|
98
110
|
case 'github_copilot':
|
|
@@ -107,6 +119,8 @@ export class AcpDetector {
|
|
|
107
119
|
return { command: 'mimo', args: ['acp'] };
|
|
108
120
|
case 'kimi_code':
|
|
109
121
|
return { command: 'kimi', args: ['acp'] };
|
|
122
|
+
case 'deepseek_harness':
|
|
123
|
+
return { command: 'dsh', args: ['--profile', 'acp'] };
|
|
110
124
|
case 'opencode':
|
|
111
125
|
default:
|
|
112
126
|
return { command: 'opencode', args: ['acp'] };
|
|
@@ -114,7 +128,8 @@ export class AcpDetector {
|
|
|
114
128
|
}
|
|
115
129
|
|
|
116
130
|
private parseVersion(output: string): string {
|
|
117
|
-
|
|
131
|
+
// 预发布段保留(如 dsh 的 `0.1.2-alpha.2`)。
|
|
132
|
+
const match = output.match(/(\d+\.\d+\.\d+(?:-[\w.]+)?)/);
|
|
118
133
|
return match ? match[1] : 'unknown';
|
|
119
134
|
}
|
|
120
135
|
}
|
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,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
|
+
});
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from 'vitest';
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
import { DshConfigError, defaultDshHome, writeDshConfig } from './dsh-config.js';
|
|
6
|
+
|
|
7
|
+
const TMP_BASE = '/tmp/opencode';
|
|
8
|
+
const API_KEY = 'zag_sk_dsh_super_secret';
|
|
9
|
+
|
|
10
|
+
const ENV = {
|
|
11
|
+
modelBaseUrl: 'http://admin-api:8002/v1',
|
|
12
|
+
modelApiKey: API_KEY,
|
|
13
|
+
modelIds: ['model-a', 'model-b'],
|
|
14
|
+
modelContextLengths: { 'model-a': 128000 },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
async function makeHome(): Promise<string> {
|
|
18
|
+
await mkdir(TMP_BASE, { recursive: true });
|
|
19
|
+
return mkdtemp(join(TMP_BASE, 'dsh-'));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function readYaml(path: string): Promise<unknown> {
|
|
23
|
+
return parse(await readFile(path, 'utf-8'));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe('writeDshConfig (T-DSH*)', () => {
|
|
27
|
+
const dirs: string[] = [];
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('defaultDshHome 优先 DSH_HOME,否则 ~/.dsh', () => {
|
|
34
|
+
expect(defaultDshHome({ DSH_HOME: ' /custom/dsh ' })).toBe('/custom/dsh');
|
|
35
|
+
expect(defaultDshHome({})).toMatch(/\/\.dsh$/);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('T-DSH1: 新建 patch 写入 4 个条目与 ZAG provider,并新建 credentials', async () => {
|
|
39
|
+
const home = await makeHome();
|
|
40
|
+
dirs.push(home);
|
|
41
|
+
|
|
42
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
43
|
+
|
|
44
|
+
expect(result).toMatchObject({
|
|
45
|
+
patchCreated: true,
|
|
46
|
+
credentialsCreated: true,
|
|
47
|
+
modelCount: 2,
|
|
48
|
+
entryCount: 4,
|
|
49
|
+
});
|
|
50
|
+
expect(result.patchPath).toBe(join(home, 'profiles', 'acp', 'cordis.patch.yml'));
|
|
51
|
+
expect(result.credentialsPath).toBe(join(home, '.credentials.yaml'));
|
|
52
|
+
|
|
53
|
+
const patch = (await readYaml(result.patchPath)) as Array<Record<string, any>>;
|
|
54
|
+
expect(patch.map((entry) => entry.id)).toEqual([
|
|
55
|
+
'llm-deepseek',
|
|
56
|
+
'llm-pi-ai',
|
|
57
|
+
'agent-default-model',
|
|
58
|
+
'acp',
|
|
59
|
+
]);
|
|
60
|
+
expect(patch[0]).toEqual({ id: 'llm-deepseek', disabled: true });
|
|
61
|
+
expect(patch[1].config.providers.zag).toMatchObject({
|
|
62
|
+
displayName: 'ZAG',
|
|
63
|
+
apiKeyEnv: 'ZAG_MODEL_API_KEY',
|
|
64
|
+
api: 'openai-completions',
|
|
65
|
+
baseURL: 'http://admin-api:8002/v1',
|
|
66
|
+
models: [
|
|
67
|
+
{ id: 'model-a', name: 'model-a', contextWindow: 128000 },
|
|
68
|
+
{ id: 'model-b', name: 'model-b' },
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
expect(patch[2]).toEqual({
|
|
72
|
+
id: 'agent-default-model',
|
|
73
|
+
config: { provider: 'zag', model: 'model-a' },
|
|
74
|
+
});
|
|
75
|
+
expect(patch[3]).toEqual({
|
|
76
|
+
id: 'acp',
|
|
77
|
+
name: '@deepseek-ai/dsh-acp',
|
|
78
|
+
config: { provider: 'zag', model: 'model-a' },
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const credentials = (await readYaml(result.credentialsPath)) as Record<string, any>;
|
|
82
|
+
expect(credentials).toEqual({ version: 1, refs: { ZAG_MODEL_API_KEY: API_KEY } });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('T-DSH2/T-DSH3: 合并时只替换我方条目(models 整体替换),其他条目与注释保留', async () => {
|
|
86
|
+
const home = await makeHome();
|
|
87
|
+
dirs.push(home);
|
|
88
|
+
const profilesDir = join(home, 'profiles', 'acp');
|
|
89
|
+
await mkdir(profilesDir, { recursive: true });
|
|
90
|
+
const patchPath = join(profilesDir, 'cordis.patch.yml');
|
|
91
|
+
await writeFile(
|
|
92
|
+
patchPath,
|
|
93
|
+
[
|
|
94
|
+
'# top comment',
|
|
95
|
+
'- id: other',
|
|
96
|
+
' value: 1 # keep inline',
|
|
97
|
+
'- id: llm-pi-ai',
|
|
98
|
+
' config:',
|
|
99
|
+
' providers:',
|
|
100
|
+
' zag:',
|
|
101
|
+
' models:',
|
|
102
|
+
' - id: old-model',
|
|
103
|
+
'- id: agent-default-model',
|
|
104
|
+
' config: { provider: old, model: old-model }',
|
|
105
|
+
'',
|
|
106
|
+
].join('\n'),
|
|
107
|
+
'utf-8',
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
await writeDshConfig({ env: ENV, dshHome: home });
|
|
111
|
+
|
|
112
|
+
const text = await readFile(patchPath, 'utf-8');
|
|
113
|
+
expect(text).toContain('# top comment');
|
|
114
|
+
expect(text).toContain('value: 1 # keep inline');
|
|
115
|
+
const patch = parse(text) as Array<Record<string, any>>;
|
|
116
|
+
expect(patch[0]).toEqual({ id: 'other', value: 1 });
|
|
117
|
+
const zag = patch.find((entry) => entry.id === 'llm-pi-ai')!.config.providers.zag;
|
|
118
|
+
expect(zag.models.map((model: { id: string }) => model.id)).toEqual(['model-a', 'model-b']);
|
|
119
|
+
expect(patch.filter((entry) => entry.id === 'llm-pi-ai')).toHaveLength(1);
|
|
120
|
+
expect(patch.filter((entry) => entry.id === 'llm-deepseek')).toHaveLength(1);
|
|
121
|
+
expect(patch.filter((entry) => entry.id === 'acp')).toHaveLength(1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('T-DSH4: contextWindow 仅在正整数时写入', async () => {
|
|
125
|
+
const home = await makeHome();
|
|
126
|
+
dirs.push(home);
|
|
127
|
+
|
|
128
|
+
await writeDshConfig({
|
|
129
|
+
env: {
|
|
130
|
+
...ENV,
|
|
131
|
+
modelContextLengths: {
|
|
132
|
+
'model-a': 0,
|
|
133
|
+
'model-b': -1,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
dshHome: home,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const patch = (await readYaml(join(home, 'profiles', 'acp', 'cordis.patch.yml'))) as Array<
|
|
140
|
+
Record<string, any>
|
|
141
|
+
>;
|
|
142
|
+
const models = patch.find((entry) => entry.id === 'llm-pi-ai')!.config.providers.zag.models;
|
|
143
|
+
expect(models).toEqual([
|
|
144
|
+
{ id: 'model-a', name: 'model-a' },
|
|
145
|
+
{ id: 'model-b', name: 'model-b' },
|
|
146
|
+
]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('T-DSH5: credentials 合并只替换 refs.ZAG_MODEL_API_KEY,保留其它 refs/records 与注释', async () => {
|
|
150
|
+
const home = await makeHome();
|
|
151
|
+
dirs.push(home);
|
|
152
|
+
await mkdir(home, { recursive: true });
|
|
153
|
+
const credentialsPath = join(home, '.credentials.yaml');
|
|
154
|
+
await writeFile(
|
|
155
|
+
credentialsPath,
|
|
156
|
+
[
|
|
157
|
+
'# credentials comment',
|
|
158
|
+
'version: 1',
|
|
159
|
+
'refs:',
|
|
160
|
+
' OTHER: keep-me',
|
|
161
|
+
'records:',
|
|
162
|
+
' github: token # keep record',
|
|
163
|
+
'',
|
|
164
|
+
].join('\n'),
|
|
165
|
+
'utf-8',
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
169
|
+
|
|
170
|
+
expect(result.credentialsCreated).toBe(false);
|
|
171
|
+
const text = await readFile(credentialsPath, 'utf-8');
|
|
172
|
+
expect(text).toContain('# credentials comment');
|
|
173
|
+
expect(text).toContain('token # keep record');
|
|
174
|
+
const credentials = parse(text) as Record<string, any>;
|
|
175
|
+
expect(credentials.version).toBe(1);
|
|
176
|
+
expect(credentials.refs).toEqual({ OTHER: 'keep-me', ZAG_MODEL_API_KEY: API_KEY });
|
|
177
|
+
expect(credentials.records).toEqual({ github: 'token' });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('T-DSH6: 目录 0700、两文件 0600', async () => {
|
|
181
|
+
const home = await makeHome();
|
|
182
|
+
dirs.push(home);
|
|
183
|
+
|
|
184
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
185
|
+
|
|
186
|
+
expect((await stat(join(home, 'profiles', 'acp'))).mode & 0o777).toBe(0o700);
|
|
187
|
+
expect((await stat(result.patchPath)).mode & 0o777).toBe(0o600);
|
|
188
|
+
expect((await stat(result.credentialsPath)).mode & 0o777).toBe(0o600);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('T-DSH8: 非法 YAML 抛错且不覆盖既有文件', async () => {
|
|
192
|
+
const home = await makeHome();
|
|
193
|
+
dirs.push(home);
|
|
194
|
+
const profilesDir = join(home, 'profiles', 'acp');
|
|
195
|
+
await mkdir(profilesDir, { recursive: true });
|
|
196
|
+
const patchPath = join(profilesDir, 'cordis.patch.yml');
|
|
197
|
+
const broken = '- id: broken\n config: [unclosed\n';
|
|
198
|
+
await writeFile(patchPath, broken, 'utf-8');
|
|
199
|
+
|
|
200
|
+
await expect(writeDshConfig({ env: ENV, dshHome: home })).rejects.toBeInstanceOf(DshConfigError);
|
|
201
|
+
expect(await readFile(patchPath, 'utf-8')).toBe(broken);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('T-DSH8b: credentials 结构非法时抛错且不写 patch', async () => {
|
|
205
|
+
const home = await makeHome();
|
|
206
|
+
dirs.push(home);
|
|
207
|
+
await mkdir(home, { recursive: true });
|
|
208
|
+
const credentialsPath = join(home, '.credentials.yaml');
|
|
209
|
+
const broken = 'version: 1\nrefs: not-a-map\n';
|
|
210
|
+
await writeFile(credentialsPath, broken, 'utf-8');
|
|
211
|
+
|
|
212
|
+
await expect(writeDshConfig({ env: ENV, dshHome: home })).rejects.toBeInstanceOf(DshConfigError);
|
|
213
|
+
expect(await readFile(credentialsPath, 'utf-8')).toBe(broken);
|
|
214
|
+
await expect(stat(join(home, 'profiles', 'acp', 'cordis.patch.yml'))).rejects.toThrow();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('T-DSH9: 空模型清单不写 model 字段、modelCount=0、不抛错', async () => {
|
|
218
|
+
const home = await makeHome();
|
|
219
|
+
dirs.push(home);
|
|
220
|
+
|
|
221
|
+
const result = await writeDshConfig({
|
|
222
|
+
env: { ...ENV, modelIds: [], modelContextLengths: {} },
|
|
223
|
+
dshHome: home,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
expect(result.modelCount).toBe(0);
|
|
227
|
+
const patch = (await readYaml(result.patchPath)) as Array<Record<string, any>>;
|
|
228
|
+
expect(patch.find((entry) => entry.id === 'agent-default-model')!.config).toEqual({
|
|
229
|
+
provider: 'zag',
|
|
230
|
+
});
|
|
231
|
+
expect(patch.find((entry) => entry.id === 'acp')!.config).toEqual({ provider: 'zag' });
|
|
232
|
+
const zag = patch.find((entry) => entry.id === 'llm-pi-ai')!.config.providers.zag;
|
|
233
|
+
expect(zag.models).toEqual([]);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('T-DSH10: 二次执行结果等价(条目不重复、权限不漂移)', async () => {
|
|
237
|
+
const home = await makeHome();
|
|
238
|
+
dirs.push(home);
|
|
239
|
+
|
|
240
|
+
const first = await writeDshConfig({ env: ENV, dshHome: home });
|
|
241
|
+
const firstPatch = await readFile(first.patchPath, 'utf-8');
|
|
242
|
+
const firstCredentials = await readFile(first.credentialsPath, 'utf-8');
|
|
243
|
+
|
|
244
|
+
const second = await writeDshConfig({ env: ENV, dshHome: home });
|
|
245
|
+
|
|
246
|
+
expect(second.patchCreated).toBe(false);
|
|
247
|
+
expect(second.credentialsCreated).toBe(false);
|
|
248
|
+
expect(await readFile(second.patchPath, 'utf-8')).toBe(firstPatch);
|
|
249
|
+
expect(await readFile(second.credentialsPath, 'utf-8')).toBe(firstCredentials);
|
|
250
|
+
const patch = parse(firstPatch) as Array<Record<string, any>>;
|
|
251
|
+
expect(patch).toHaveLength(4);
|
|
252
|
+
expect((await stat(second.patchPath)).mode & 0o777).toBe(0o600);
|
|
253
|
+
expect((await stat(join(home, 'profiles', 'acp'))).mode & 0o777).toBe(0o700);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('T-DSH11: 返回值/错误信息不含 API Key', async () => {
|
|
257
|
+
const home = await makeHome();
|
|
258
|
+
dirs.push(home);
|
|
259
|
+
|
|
260
|
+
const result = await writeDshConfig({ env: ENV, dshHome: home });
|
|
261
|
+
expect(JSON.stringify(result)).not.toContain(API_KEY);
|
|
262
|
+
|
|
263
|
+
const brokenHome = await makeHome();
|
|
264
|
+
dirs.push(brokenHome);
|
|
265
|
+
const profilesDir = join(brokenHome, 'profiles', 'acp');
|
|
266
|
+
await mkdir(profilesDir, { recursive: true });
|
|
267
|
+
await writeFile(join(profilesDir, 'cordis.patch.yml'), '- broken: [\n', 'utf-8');
|
|
268
|
+
try {
|
|
269
|
+
await writeDshConfig({ env: ENV, dshHome: brokenHome });
|
|
270
|
+
expect.unreachable();
|
|
271
|
+
} catch (error) {
|
|
272
|
+
expect(JSON.stringify((error as Error).message)).not.toContain(API_KEY);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
});
|