agent-gear 2.19.0 → 2.21.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 +50 -10
- package/dist/cloud-host/supervisor.js +1 -1
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +24 -0
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/harness/dsh-config.d.ts +11 -1
- package/dist/harness/dsh-config.d.ts.map +1 -1
- package/dist/harness/dsh-config.js +44 -18
- package/dist/harness/dsh-config.js.map +1 -1
- package/dist/harness/dsh-config.test.js +150 -1
- package/dist/harness/dsh-config.test.js.map +1 -1
- package/dist/harness/env.d.ts +10 -2
- package/dist/harness/env.d.ts.map +1 -1
- package/dist/harness/env.js +30 -5
- package/dist/harness/env.js.map +1 -1
- package/dist/harness/env.test.js +64 -0
- package/dist/harness/env.test.js.map +1 -1
- package/dist/harness/model-credentials.d.ts +26 -0
- package/dist/harness/model-credentials.d.ts.map +1 -0
- package/dist/harness/model-credentials.js +108 -0
- package/dist/harness/model-credentials.js.map +1 -0
- package/dist/harness/model-credentials.test.d.ts +2 -0
- package/dist/harness/model-credentials.test.d.ts.map +1 -0
- package/dist/harness/model-credentials.test.js +80 -0
- package/dist/harness/model-credentials.test.js.map +1 -0
- package/dist/harness/model-providers.d.ts +107 -0
- package/dist/harness/model-providers.d.ts.map +1 -0
- package/dist/harness/model-providers.js +197 -0
- package/dist/harness/model-providers.js.map +1 -0
- package/dist/harness/model-providers.test.d.ts +2 -0
- package/dist/harness/model-providers.test.d.ts.map +1 -0
- package/dist/harness/model-providers.test.js +216 -0
- package/dist/harness/model-providers.test.js.map +1 -0
- package/dist/harness/opencode-config.d.ts +41 -8
- package/dist/harness/opencode-config.d.ts.map +1 -1
- package/dist/harness/opencode-config.js +133 -12
- package/dist/harness/opencode-config.js.map +1 -1
- package/dist/harness/opencode-config.test.js +227 -50
- package/dist/harness/opencode-config.test.js.map +1 -1
- package/dist/harness/prepare.d.ts +2 -0
- package/dist/harness/prepare.d.ts.map +1 -1
- package/dist/harness/prepare.js +17 -2
- package/dist/harness/prepare.js.map +1 -1
- package/dist/harness/prepare.test.js +33 -0
- package/dist/harness/prepare.test.js.map +1 -1
- package/dist/knowledge-bases.d.ts +7 -15
- package/dist/knowledge-bases.d.ts.map +1 -1
- package/dist/knowledge-bases.js +40 -70
- package/dist/knowledge-bases.js.map +1 -1
- package/dist/knowledge-bases.test.js +79 -60
- package/dist/knowledge-bases.test.js.map +1 -1
- package/dist/socketio-client.d.ts +11 -0
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.js +94 -0
- package/dist/socketio-client.js.map +1 -1
- package/dist/socketio-client.test.js +128 -0
- package/dist/socketio-client.test.js.map +1 -1
- package/package.json +1 -1
- package/src/cloud-host/supervisor.test.ts +32 -0
- package/src/cloud-host/supervisor.ts +1 -1
- package/src/harness/dsh-config.test.ts +175 -1
- package/src/harness/dsh-config.ts +77 -23
- package/src/harness/env.test.ts +74 -0
- package/src/harness/env.ts +41 -5
- package/src/harness/model-credentials.test.ts +133 -0
- package/src/harness/model-credentials.ts +119 -0
- package/src/harness/model-providers.test.ts +277 -0
- package/src/harness/model-providers.ts +289 -0
- package/src/harness/opencode-config.test.ts +278 -57
- package/src/harness/opencode-config.ts +180 -20
- package/src/harness/prepare.test.ts +36 -0
- package/src/harness/prepare.ts +20 -2
- package/src/knowledge-bases.test.ts +93 -65
- package/src/knowledge-bases.ts +43 -78
- package/src/socketio-client.test.ts +153 -0
- package/src/socketio-client.ts +122 -0
- package/tests/agent-gear-upgrade.test.ts +285 -0
package/src/knowledge-bases.ts
CHANGED
|
@@ -9,9 +9,9 @@ import { join, resolve, sep } from 'node:path';
|
|
|
9
9
|
* 含常规文件 `knowledge-base.json` 即识别),库内文档递归收集 `.md` 常规文件并读取内容。
|
|
10
10
|
* 仅读不写、不执行;不使用会话路径白名单。
|
|
11
11
|
*
|
|
12
|
-
* 上限(与 agent-api 编写指引 `knowledge-base-prompts.ts`
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* 上限(与 agent-api 编写指引 `knowledge-base-prompts.ts` 同源,二期口径):
|
|
13
|
+
* 单文件(`knowledge-base.json` 与 `.md`)1MiB / 单文件夹直接子项(目录与文件合计)1000;
|
|
14
|
+
* 无知识库数、文档数与内容累计总量限制。truncated 按「文件夹 → 知识库 → 来源 → 整体」逐级上浮。
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
export const REPO_KNOWLEDGE_BASE_SOURCES = [
|
|
@@ -21,18 +21,10 @@ export const REPO_KNOWLEDGE_BASE_SOURCES = [
|
|
|
21
21
|
|
|
22
22
|
export type RepoKnowledgeBaseSource = (typeof REPO_KNOWLEDGE_BASE_SOURCES)[number]['source'];
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
export const
|
|
26
|
-
/**
|
|
27
|
-
export const
|
|
28
|
-
/** knowledge-base.json 读取上限(字节) */
|
|
29
|
-
export const MAX_SCHEMA_BYTES = 64 * 1024;
|
|
30
|
-
/** 单文档读取上限(字节) */
|
|
31
|
-
export const MAX_DOCUMENT_BYTES = 128 * 1024;
|
|
32
|
-
/** 单知识库内容累计上限(字节) */
|
|
33
|
-
export const MAX_BASE_TOTAL_BYTES = 1024 * 1024;
|
|
34
|
-
/** 单来源内容累计上限(字节) */
|
|
35
|
-
export const MAX_SOURCE_TOTAL_BYTES = 2 * 1024 * 1024;
|
|
24
|
+
/** 单文件读取上限(字节):`knowledge-base.json` 与 `.md` 同值 */
|
|
25
|
+
export const MAX_FILE_BYTES = 1024 * 1024;
|
|
26
|
+
/** 每个文件夹直接子项上限(目录与文件合计) */
|
|
27
|
+
export const MAX_ENTRIES_PER_FOLDER = 1000;
|
|
36
28
|
|
|
37
29
|
export const KNOWLEDGE_BASE_IDENTIFIER_FILE = 'knowledge-base.json';
|
|
38
30
|
|
|
@@ -91,8 +83,11 @@ function toPosixPath(value: string): string {
|
|
|
91
83
|
}
|
|
92
84
|
|
|
93
85
|
/** 递归收集 `.md` 常规文件(符号链接仅接受指向常规文件的链接;目录链接不跟随)。 */
|
|
94
|
-
async function collectMarkdownFiles(
|
|
95
|
-
|
|
86
|
+
async function collectMarkdownFiles(
|
|
87
|
+
baseDir: string,
|
|
88
|
+
): Promise<{ files: DocCandidate[]; truncated: boolean }> {
|
|
89
|
+
const files: DocCandidate[] = [];
|
|
90
|
+
let truncated = false;
|
|
96
91
|
|
|
97
92
|
const walk = async (dir: string, prefix: string): Promise<void> => {
|
|
98
93
|
let entries;
|
|
@@ -103,6 +98,12 @@ async function collectMarkdownFiles(baseDir: string): Promise<DocCandidate[]> {
|
|
|
103
98
|
// 单文档读取失败才逐条剔除。避免仓库权限细节放大为库级错误。
|
|
104
99
|
return;
|
|
105
100
|
}
|
|
101
|
+
// 每层按名称排序后取前 1000(目录与文件合计),超出部分不遍历(二期 D3)
|
|
102
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
103
|
+
if (entries.length > MAX_ENTRIES_PER_FOLDER) {
|
|
104
|
+
entries = entries.slice(0, MAX_ENTRIES_PER_FOLDER);
|
|
105
|
+
truncated = true;
|
|
106
|
+
}
|
|
106
107
|
for (const entry of entries) {
|
|
107
108
|
const fullPath = join(dir, entry.name);
|
|
108
109
|
const relPath = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
@@ -114,31 +115,25 @@ async function collectMarkdownFiles(baseDir: string): Promise<DocCandidate[]> {
|
|
|
114
115
|
if (!entry.name.toLowerCase().endsWith('.md')) continue;
|
|
115
116
|
try {
|
|
116
117
|
const target = await stat(fullPath);
|
|
117
|
-
if (target.isFile())
|
|
118
|
+
if (target.isFile()) files.push({ fullPath, relPath });
|
|
118
119
|
} catch {
|
|
119
120
|
// 悬空/不可解析的符号链接:跳过
|
|
120
121
|
}
|
|
121
122
|
continue;
|
|
122
123
|
}
|
|
123
124
|
if (entry.isFile() && entry.name.toLowerCase().endsWith('.md')) {
|
|
124
|
-
|
|
125
|
+
files.push({ fullPath, relPath });
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
};
|
|
128
129
|
|
|
129
130
|
await walk(baseDir, '');
|
|
130
|
-
return
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
interface SourceScanState {
|
|
134
|
-
sourceBytes: number;
|
|
135
|
-
truncated: boolean;
|
|
131
|
+
return { files, truncated };
|
|
136
132
|
}
|
|
137
133
|
|
|
138
134
|
async function scanKnowledgeBase(
|
|
139
135
|
sourceDir: string,
|
|
140
136
|
folder: string,
|
|
141
|
-
state: SourceScanState,
|
|
142
137
|
): Promise<KnowledgeBaseEntry> {
|
|
143
138
|
const baseDir = join(sourceDir, folder);
|
|
144
139
|
const schemaPath = join(baseDir, KNOWLEDGE_BASE_IDENTIFIER_FILE);
|
|
@@ -154,7 +149,7 @@ async function scanKnowledgeBase(
|
|
|
154
149
|
// 仅常规文件(或指向常规文件的符号链接)才识别为知识库
|
|
155
150
|
if (!schemaInfo.isFile()) return { folder };
|
|
156
151
|
|
|
157
|
-
if (schemaInfo.size >
|
|
152
|
+
if (schemaInfo.size > MAX_FILE_BYTES) {
|
|
158
153
|
return { folder, error: 'file too large' };
|
|
159
154
|
}
|
|
160
155
|
|
|
@@ -165,59 +160,35 @@ async function scanKnowledgeBase(
|
|
|
165
160
|
return { folder, error: mapFileError(err) };
|
|
166
161
|
}
|
|
167
162
|
const schemaBytes = Buffer.byteLength(schema, 'utf8');
|
|
168
|
-
if (schemaBytes >
|
|
163
|
+
if (schemaBytes > MAX_FILE_BYTES) {
|
|
169
164
|
return { folder, error: 'file too large' };
|
|
170
165
|
}
|
|
171
166
|
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
let truncated = false;
|
|
177
|
-
if (candidates.length > MAX_DOCUMENTS_PER_BASE) {
|
|
178
|
-
candidates.length = MAX_DOCUMENTS_PER_BASE;
|
|
179
|
-
truncated = true;
|
|
180
|
-
}
|
|
167
|
+
const collected = await collectMarkdownFiles(baseDir);
|
|
168
|
+
const candidates = collected.files.sort((a, b) => a.relPath.localeCompare(b.relPath));
|
|
181
169
|
|
|
182
170
|
const documents: KnowledgeBaseDocumentFile[] = [];
|
|
183
|
-
let baseBytes = 0;
|
|
184
|
-
|
|
185
171
|
for (const candidate of candidates) {
|
|
186
|
-
if (state.truncated) break;
|
|
187
172
|
try {
|
|
188
173
|
const info = await stat(candidate.fullPath);
|
|
189
|
-
if (info.size >
|
|
190
|
-
//
|
|
174
|
+
if (info.size > MAX_FILE_BYTES) {
|
|
175
|
+
// 不读内容;单篇超限不影响后续文档(保留条目并附白名单文案)
|
|
191
176
|
documents.push({ path: candidate.relPath, error: 'file too large' });
|
|
192
177
|
continue;
|
|
193
178
|
}
|
|
194
|
-
if (baseBytes + info.size > MAX_BASE_TOTAL_BYTES) {
|
|
195
|
-
truncated = true;
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
if (state.sourceBytes + info.size > MAX_SOURCE_TOTAL_BYTES) {
|
|
199
|
-
state.truncated = true;
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
179
|
const content = await readFile(candidate.fullPath, { encoding: 'utf8' });
|
|
203
|
-
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
if (state.sourceBytes + bytes > MAX_SOURCE_TOTAL_BYTES) {
|
|
209
|
-
state.truncated = true;
|
|
210
|
-
break;
|
|
180
|
+
// 读后复核(防 stat 失真);超限仍保留条目、不返回内容
|
|
181
|
+
if (Buffer.byteLength(content, 'utf8') > MAX_FILE_BYTES) {
|
|
182
|
+
documents.push({ path: candidate.relPath, error: 'file too large' });
|
|
183
|
+
continue;
|
|
211
184
|
}
|
|
212
|
-
baseBytes += bytes;
|
|
213
|
-
state.sourceBytes += bytes;
|
|
214
185
|
documents.push({ path: candidate.relPath, content });
|
|
215
186
|
} catch (err) {
|
|
216
187
|
documents.push({ path: candidate.relPath, error: mapFileError(err) });
|
|
217
188
|
}
|
|
218
189
|
}
|
|
219
190
|
|
|
220
|
-
return { folder, schema, documents, truncated };
|
|
191
|
+
return { folder, schema, documents, truncated: collected.truncated };
|
|
221
192
|
}
|
|
222
193
|
|
|
223
194
|
async function scanSource(
|
|
@@ -239,8 +210,17 @@ async function scanSource(
|
|
|
239
210
|
return null;
|
|
240
211
|
}
|
|
241
212
|
|
|
213
|
+
const ordered = [...entries].sort((a, b) => a.name.localeCompare(b.name));
|
|
214
|
+
// 来源目录层与库内目录层同构:直接子项(目录与文件合计)按名称取前 1000,
|
|
215
|
+
// 窗口外不遍历并置来源 truncated;再从窗口内筛选目录/目录符号链接作为知识库候选。
|
|
216
|
+
let truncated = false;
|
|
217
|
+
if (ordered.length > MAX_ENTRIES_PER_FOLDER) {
|
|
218
|
+
ordered.length = MAX_ENTRIES_PER_FOLDER;
|
|
219
|
+
truncated = true;
|
|
220
|
+
}
|
|
221
|
+
|
|
242
222
|
const folders: string[] = [];
|
|
243
|
-
for (const entry of
|
|
223
|
+
for (const entry of ordered) {
|
|
244
224
|
if (entry.isDirectory()) {
|
|
245
225
|
folders.push(entry.name);
|
|
246
226
|
continue;
|
|
@@ -254,29 +234,14 @@ async function scanSource(
|
|
|
254
234
|
}
|
|
255
235
|
}
|
|
256
236
|
|
|
257
|
-
folders.sort((a, b) => a.localeCompare(b));
|
|
258
|
-
let truncated = false;
|
|
259
|
-
if (folders.length > MAX_KNOWLEDGE_BASES_PER_SOURCE) {
|
|
260
|
-
folders.length = MAX_KNOWLEDGE_BASES_PER_SOURCE;
|
|
261
|
-
truncated = true;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const state: SourceScanState = { sourceBytes: 0, truncated: false };
|
|
265
237
|
const knowledgeBases: KnowledgeBaseEntry[] = [];
|
|
266
238
|
for (const folder of folders) {
|
|
267
|
-
|
|
268
|
-
truncated = true;
|
|
269
|
-
break;
|
|
270
|
-
}
|
|
271
|
-
const entry = await scanKnowledgeBase(sourceDir, folder, state);
|
|
239
|
+
const entry = await scanKnowledgeBase(sourceDir, folder);
|
|
272
240
|
// 未识别为知识库(无 knowledge-base.json)的文件夹不输出
|
|
273
241
|
if (entry.schema === undefined && entry.error === undefined) continue;
|
|
274
|
-
// 单来源 2MiB 截断发生在该库读取过程中时,同样置位条目级 truncated(与单库 1MiB 口径对称)
|
|
275
|
-
if (state.truncated) entry.truncated = true;
|
|
276
242
|
if (entry.truncated) truncated = true;
|
|
277
243
|
knowledgeBases.push(entry);
|
|
278
244
|
}
|
|
279
|
-
if (state.truncated) truncated = true;
|
|
280
245
|
|
|
281
246
|
return { source, knowledgeBases, truncated };
|
|
282
247
|
}
|
|
@@ -701,3 +701,156 @@ describe('knowledge-bases:scan 事件注册(契约 §3.29/§3.30)', () => {
|
|
|
701
701
|
}
|
|
702
702
|
});
|
|
703
703
|
});
|
|
704
|
+
|
|
705
|
+
describe('harness:models 运行期模型下发(T-AG-MP4)', () => {
|
|
706
|
+
const PROVIDER_ID = '65f0a1b2c3d4e5f60718293a';
|
|
707
|
+
const PROVISION = {
|
|
708
|
+
providers: [
|
|
709
|
+
{
|
|
710
|
+
vendorId: 'zag',
|
|
711
|
+
displayName: 'Agent Gear',
|
|
712
|
+
models: [{ id: 'model-a', contextLength: 128000 }],
|
|
713
|
+
},
|
|
714
|
+
{ vendorId: PROVIDER_ID, displayName: '我的 OpenAI', models: [{ id: 'gpt-5.1' }] },
|
|
715
|
+
],
|
|
716
|
+
};
|
|
717
|
+
|
|
718
|
+
const dirs: string[] = [];
|
|
719
|
+
const savedEnv = {
|
|
720
|
+
baseUrl: process.env.ZAG_MODEL_BASE_URL,
|
|
721
|
+
apiKey: process.env.ZAG_MODEL_API_KEY,
|
|
722
|
+
types: process.env.ZAG_HARNESS_TYPES,
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
function setupWorker(paths: { configDir: string; dshHome: string; statePath: string }) {
|
|
726
|
+
connectToServer('http://localhost:4001', 'controller-key', {
|
|
727
|
+
serverName: 'test-controller',
|
|
728
|
+
handshakeToken: 'controller-key',
|
|
729
|
+
skipLocalMcpStart: true,
|
|
730
|
+
handleWorkload: true,
|
|
731
|
+
persistConfig: false,
|
|
732
|
+
onShutdown: vi.fn(),
|
|
733
|
+
harnessModelPaths: paths,
|
|
734
|
+
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
async function makePaths() {
|
|
739
|
+
const { mkdtempSync } = await import('fs');
|
|
740
|
+
const { join } = await import('path');
|
|
741
|
+
const { tmpdir } = await import('os');
|
|
742
|
+
const dir = mkdtempSync(join(tmpdir(), 'sio-hm-'));
|
|
743
|
+
dirs.push(dir);
|
|
744
|
+
return {
|
|
745
|
+
configDir: join(dir, 'config'),
|
|
746
|
+
dshHome: join(dir, 'dsh'),
|
|
747
|
+
statePath: join(dir, 'state.json'),
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
beforeEach(() => {
|
|
752
|
+
fake.handlers.clear();
|
|
753
|
+
fake.socket.emit.mockClear();
|
|
754
|
+
process.env.ZAG_MODEL_BASE_URL = 'http://admin-api:8002/v1';
|
|
755
|
+
process.env.ZAG_MODEL_API_KEY = 'zag_sk_super_secret';
|
|
756
|
+
process.env.ZAG_HARNESS_TYPES = 'opencode';
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
afterEach(async () => {
|
|
760
|
+
const { rmSync } = await import('fs');
|
|
761
|
+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
762
|
+
if (savedEnv.baseUrl === undefined) delete process.env.ZAG_MODEL_BASE_URL;
|
|
763
|
+
else process.env.ZAG_MODEL_BASE_URL = savedEnv.baseUrl;
|
|
764
|
+
if (savedEnv.apiKey === undefined) delete process.env.ZAG_MODEL_API_KEY;
|
|
765
|
+
else process.env.ZAG_MODEL_API_KEY = savedEnv.apiKey;
|
|
766
|
+
if (savedEnv.types === undefined) delete process.env.ZAG_HARNESS_TYPES;
|
|
767
|
+
else process.env.ZAG_HARNESS_TYPES = savedEnv.types;
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
it('T-AG-MP4: applies every vendor and replies success with appliedTypes', async () => {
|
|
771
|
+
const paths = await makePaths();
|
|
772
|
+
setupWorker(paths);
|
|
773
|
+
const handler = fake.handlers.get('harness:models') as (
|
|
774
|
+
payload: unknown,
|
|
775
|
+
) => Promise<void>;
|
|
776
|
+
expect(handler).toBeDefined();
|
|
777
|
+
|
|
778
|
+
await handler({ requestId: 'r-hm1', provision: PROVISION });
|
|
779
|
+
|
|
780
|
+
expect(fake.socket.emit).toHaveBeenCalledWith('harness:models:result', {
|
|
781
|
+
requestId: 'r-hm1',
|
|
782
|
+
success: true,
|
|
783
|
+
appliedTypes: ['opencode'],
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
const { readFile } = await import('fs/promises');
|
|
787
|
+
const { parse } = await import('jsonc-parser');
|
|
788
|
+
const { join } = await import('path');
|
|
789
|
+
const config = parse(await readFile(join(paths.configDir, 'opencode.jsonc'), 'utf-8'));
|
|
790
|
+
expect(config.providers.zag.name).toBe('Agent Gear');
|
|
791
|
+
expect(config.providers[PROVIDER_ID].name).toBe('我的 OpenAI');
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
it('T-AG-MP4: an invalid payload still replies success:false (no empty wait)', async () => {
|
|
795
|
+
const paths = await makePaths();
|
|
796
|
+
setupWorker(paths);
|
|
797
|
+
const handler = fake.handlers.get('harness:models') as (
|
|
798
|
+
payload: unknown,
|
|
799
|
+
) => Promise<void>;
|
|
800
|
+
|
|
801
|
+
await handler({ requestId: 'r-bad', provision: { providers: [{ vendorId: '' }] } });
|
|
802
|
+
|
|
803
|
+
expect(fake.socket.emit).toHaveBeenCalledWith(
|
|
804
|
+
'harness:models:result',
|
|
805
|
+
expect.objectContaining({
|
|
806
|
+
requestId: 'r-bad',
|
|
807
|
+
success: false,
|
|
808
|
+
appliedTypes: [],
|
|
809
|
+
error: expect.stringContaining('providers[0].vendorId'),
|
|
810
|
+
}),
|
|
811
|
+
);
|
|
812
|
+
expect(JSON.stringify(fake.socket.emit.mock.calls)).not.toContain('zag_sk_super_secret');
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
it('T-AG-MP4: a failing type keeps the other type applied; error is redacted', async () => {
|
|
816
|
+
const { mkdirSync, writeFileSync } = await import('fs');
|
|
817
|
+
const { join } = await import('path');
|
|
818
|
+
const paths = await makePaths();
|
|
819
|
+
mkdirSync(join(paths.dshHome, 'profiles', 'acp'), { recursive: true });
|
|
820
|
+
writeFileSync(join(paths.dshHome, 'profiles', 'acp', 'cordis.patch.yml'), '- broken: [\n');
|
|
821
|
+
process.env.ZAG_HARNESS_TYPES = 'opencode,deepseek_harness';
|
|
822
|
+
setupWorker(paths);
|
|
823
|
+
const handler = fake.handlers.get('harness:models') as (
|
|
824
|
+
payload: unknown,
|
|
825
|
+
) => Promise<void>;
|
|
826
|
+
|
|
827
|
+
await handler({ requestId: 'r-partial', provision: PROVISION });
|
|
828
|
+
|
|
829
|
+
const call = fake.socket.emit.mock.calls.find(
|
|
830
|
+
([event]) => event === 'harness:models:result',
|
|
831
|
+
) as [string, { success: boolean; appliedTypes: string[]; error?: string }];
|
|
832
|
+
expect(call[1].success).toBe(false);
|
|
833
|
+
expect(call[1].appliedTypes).toEqual(['opencode']);
|
|
834
|
+
expect(call[1].error).toContain('deepseek_harness');
|
|
835
|
+
expect(call[1].error).not.toContain('zag_sk_super_secret');
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
it('T-AG-MP4: the standard (controller-api) connection never responds', async () => {
|
|
839
|
+
const paths = await makePaths();
|
|
840
|
+
connectToServer('http://localhost:4001', 'controller-key', {
|
|
841
|
+
serverName: 'test-controller',
|
|
842
|
+
handshakeToken: 'controller-key',
|
|
843
|
+
skipLocalMcpStart: true,
|
|
844
|
+
handleWorkload: false,
|
|
845
|
+
persistConfig: false,
|
|
846
|
+
harnessModelPaths: paths,
|
|
847
|
+
});
|
|
848
|
+
const handler = fake.handlers.get('harness:models') as (
|
|
849
|
+
payload: unknown,
|
|
850
|
+
) => Promise<void>;
|
|
851
|
+
|
|
852
|
+
await handler({ requestId: 'r-std', provision: PROVISION });
|
|
853
|
+
|
|
854
|
+
expect(fake.socket.emit).not.toHaveBeenCalled();
|
|
855
|
+
});
|
|
856
|
+
});
|
package/src/socketio-client.ts
CHANGED
|
@@ -18,6 +18,9 @@ import { worktreeHandler } from './worktree-handler.js';
|
|
|
18
18
|
import { fsHandler } from './fs-handler.js';
|
|
19
19
|
import { scanWorkflowTemplates } from './workflow-templates.js';
|
|
20
20
|
import { scanKnowledgeBases } from './knowledge-bases.js';
|
|
21
|
+
import { resolvePrepareTypes } from './harness/env.js';
|
|
22
|
+
import { resolveConfiguredCredentials } from './harness/model-credentials.js';
|
|
23
|
+
import { applyModelProvision, parseModelProvision } from './harness/model-providers.js';
|
|
21
24
|
import { gatewayManager } from './gateway.js';
|
|
22
25
|
import { processRegistry, isProcessAlive } from './acp/process-registry.js';
|
|
23
26
|
import { McpRelay } from './mcp/relay.js';
|
|
@@ -38,6 +41,9 @@ const { version: CONTROLLER_VERSION } = createRequire(import.meta.url)('../packa
|
|
|
38
41
|
const workloadExecutor = new WorkloadExecutor();
|
|
39
42
|
// shutdown 到达时若仍有 post 批次在跑,最多等待该时长后退出(契约 §2.8)。
|
|
40
43
|
const SHUTDOWN_WORKLOAD_WAIT_MS = 5000;
|
|
44
|
+
// 升级回执 emit 后延迟约 100ms 再优雅停机(D5):保证 ack flush 出 socket 缓冲,
|
|
45
|
+
// 避免服务端误判回执超时(`UPGRADE_NOT_ACCEPTED`)而用户侧已重启。
|
|
46
|
+
const AGENT_GEAR_UPGRADE_SHUTDOWN_DELAY_MS = 100;
|
|
41
47
|
// 云电脑模式 `KEY_ALREADY_ONLINE` 退避重连:断开后手动重连(1s→2s→…→30s)。
|
|
42
48
|
// 非云电脑模式保持既有行为(仅断开,不做额外兜底)。
|
|
43
49
|
const CLOUD_HOST_RECONNECT_BASE_MS = 1000;
|
|
@@ -155,6 +161,15 @@ export interface ConnectToServerOptions {
|
|
|
155
161
|
onShutdown?: (payload: ShutdownPayload) => void;
|
|
156
162
|
/** 注入 logger(测试用);缺省进程级单例,统一 `component: 'socketio-client'`。 */
|
|
157
163
|
logger?: ObservationLogger;
|
|
164
|
+
/**
|
|
165
|
+
* 运行期模型下发(`harness:models`)的写盘目录覆盖(测试/定制场景)。
|
|
166
|
+
* 缺省 `~/.config/opencode`、`~/.dsh` 与 `~/.zag/model-providers-state.json`。
|
|
167
|
+
*/
|
|
168
|
+
harnessModelPaths?: {
|
|
169
|
+
configDir?: string;
|
|
170
|
+
dshHome?: string;
|
|
171
|
+
statePath?: string;
|
|
172
|
+
};
|
|
158
173
|
}
|
|
159
174
|
|
|
160
175
|
interface ErrorPayload {
|
|
@@ -164,6 +179,8 @@ interface ErrorPayload {
|
|
|
164
179
|
|
|
165
180
|
interface ShutdownPayload {
|
|
166
181
|
reason: string;
|
|
182
|
+
/** 显式退出码(云电脑升级事件传 0;缺省按 reason 分类,见 exit-codes.ts)。 */
|
|
183
|
+
exitCode?: number;
|
|
167
184
|
}
|
|
168
185
|
|
|
169
186
|
interface AcpCheckPayload {
|
|
@@ -495,6 +512,8 @@ export function connectToServer(
|
|
|
495
512
|
let reconnectAttempt = 0;
|
|
496
513
|
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
497
514
|
let shutdownStarted = false;
|
|
515
|
+
// 升级事件幂等守卫:已接受过升级后再次收到事件只回执拒绝,不重复停机。
|
|
516
|
+
let upgradeAccepted = false;
|
|
498
517
|
|
|
499
518
|
const clearReconnectTimer = (): void => {
|
|
500
519
|
if (reconnectTimer) {
|
|
@@ -661,6 +680,38 @@ export function connectToServer(
|
|
|
661
680
|
}
|
|
662
681
|
});
|
|
663
682
|
|
|
683
|
+
// 云电脑升级事件(契约 `cloud-controller-websocket.md` §2.11/§2.12,agent-gear 2.20.0 起)。
|
|
684
|
+
// worker **不执行安装**:回执后延迟 ~100ms(D5,保证 ack flush)再优雅退出 0,
|
|
685
|
+
// 由入口保活链路完成「钉版安装 → 以新版本重启」。本地模式同样注册但事件永不到达
|
|
686
|
+
// (controller-api 从不下发),仅保留 `process.exit(0)` 死路径。
|
|
687
|
+
socket.on('agent-gear:upgrade', (data?: { targetVersion?: string }) => {
|
|
688
|
+
const targetVersion = typeof data?.targetVersion === 'string' ? data.targetVersion : undefined;
|
|
689
|
+
if (upgradeAccepted || shutdownStarted) {
|
|
690
|
+
log.info(
|
|
691
|
+
{ event: 'agent_gear_upgrade', targetVersion, currentVersion: CONTROLLER_VERSION, accepted: false },
|
|
692
|
+
'agent-gear upgrade rejected: shutting down',
|
|
693
|
+
);
|
|
694
|
+
socket.emit('agent-gear:upgrade:result', { accepted: false, reason: 'shutting_down' });
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
upgradeAccepted = true;
|
|
698
|
+
log.info(
|
|
699
|
+
{ event: 'agent_gear_upgrade', targetVersion, currentVersion: CONTROLLER_VERSION, accepted: true },
|
|
700
|
+
'agent-gear upgrade accepted',
|
|
701
|
+
);
|
|
702
|
+
socket.emit('agent-gear:upgrade:result', { accepted: true, currentVersion: CONTROLLER_VERSION });
|
|
703
|
+
setTimeout(() => {
|
|
704
|
+
if (shutdownStarted) return;
|
|
705
|
+
if (options?.onShutdown) {
|
|
706
|
+
// 云电脑模式:由 supervisor 关闭双连接,以退出码 0 结束(入口保活重启并钉版安装)。
|
|
707
|
+
notifyShutdown({ reason: 'agent-gear upgrade', exitCode: 0 });
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
// 本地模式死路径:复用既有「信号退出 0」语义,不新增退出码常量。
|
|
711
|
+
process.exit(0);
|
|
712
|
+
}, AGENT_GEAR_UPGRADE_SHUTDOWN_DELAY_MS);
|
|
713
|
+
});
|
|
714
|
+
|
|
664
715
|
// Workload events
|
|
665
716
|
socket.on('workload:run', (data: WorkloadRunPayload) => {
|
|
666
717
|
if (!handleWorkload) return;
|
|
@@ -1580,6 +1631,77 @@ export function connectToServer(
|
|
|
1580
1631
|
socket.emit('knowledge-bases:scan:result', { requestId: data.requestId, ...result });
|
|
1581
1632
|
});
|
|
1582
1633
|
|
|
1634
|
+
// 运行期模型下发(契约 §2.13/§2.14):全量替换 opencode/dsh 的模型配置。
|
|
1635
|
+
// 仅 `/worker` 连接处理(handleWorkload=true);标准连接零响应。
|
|
1636
|
+
socket.on('harness:models', async (data: { requestId?: unknown; provision?: unknown }) => {
|
|
1637
|
+
if (!handleWorkload) return;
|
|
1638
|
+
const requestId = typeof data?.requestId === 'string' ? data.requestId : '';
|
|
1639
|
+
log.info({ event: 'harness_models', requestId }, 'harness:models received');
|
|
1640
|
+
|
|
1641
|
+
const secrets = collectSecretValues(process.env);
|
|
1642
|
+
const fail = (message: string, appliedTypes: string[] = []): void => {
|
|
1643
|
+
// 错误文本可能含调用栈/上游片段:统一脱敏后再回包(契约 §2.14)。
|
|
1644
|
+
const error = redactSecrets(message, secrets);
|
|
1645
|
+
log.warn({ event: 'harness_models_failed', requestId, error }, 'harness:models failed');
|
|
1646
|
+
socket.emit('harness:models:result', {
|
|
1647
|
+
requestId,
|
|
1648
|
+
success: false,
|
|
1649
|
+
appliedTypes,
|
|
1650
|
+
error,
|
|
1651
|
+
});
|
|
1652
|
+
};
|
|
1653
|
+
|
|
1654
|
+
let provision: ReturnType<typeof parseModelProvision>;
|
|
1655
|
+
try {
|
|
1656
|
+
// 载荷非法也回报 success:false,避免服务端空等(契约 §2.13)。
|
|
1657
|
+
provision = parseModelProvision(data?.provision);
|
|
1658
|
+
} catch (error) {
|
|
1659
|
+
fail((error as Error).message);
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
const env = await resolveConfiguredCredentials({
|
|
1664
|
+
configDir: options?.harnessModelPaths?.configDir,
|
|
1665
|
+
dshHome: options?.harnessModelPaths?.dshHome,
|
|
1666
|
+
});
|
|
1667
|
+
if (env.modelApiKey) {
|
|
1668
|
+
// 配置回读的凭据不在进程 env 中:并入脱敏集合,避免错误文本带出原值。
|
|
1669
|
+
secrets.push(env.modelApiKey);
|
|
1670
|
+
}
|
|
1671
|
+
if (!env.modelBaseUrl || !env.modelApiKey) {
|
|
1672
|
+
fail('missing required environment variable(s): ZAG_MODEL_BASE_URL, ZAG_MODEL_API_KEY');
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
const result = await applyModelProvision({
|
|
1677
|
+
provision,
|
|
1678
|
+
env,
|
|
1679
|
+
types: resolvePrepareTypes(process.env),
|
|
1680
|
+
configDir: options?.harnessModelPaths?.configDir,
|
|
1681
|
+
dshHome: options?.harnessModelPaths?.dshHome,
|
|
1682
|
+
statePath: options?.harnessModelPaths?.statePath,
|
|
1683
|
+
});
|
|
1684
|
+
|
|
1685
|
+
if (result.errors.length > 0) {
|
|
1686
|
+
fail(
|
|
1687
|
+
result.errors.map((item) => `${item.type}: ${item.message}`).join('; '),
|
|
1688
|
+
result.appliedTypes,
|
|
1689
|
+
);
|
|
1690
|
+
return;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
// 只记录类型与数量,不记录模型名/凭据(契约 §2.13)。
|
|
1694
|
+
log.info(
|
|
1695
|
+
{ event: 'harness_models_applied', requestId, appliedTypes: result.appliedTypes },
|
|
1696
|
+
'harness:models applied',
|
|
1697
|
+
);
|
|
1698
|
+
socket.emit('harness:models:result', {
|
|
1699
|
+
requestId,
|
|
1700
|
+
success: true,
|
|
1701
|
+
appliedTypes: result.appliedTypes,
|
|
1702
|
+
});
|
|
1703
|
+
});
|
|
1704
|
+
|
|
1583
1705
|
// Reconnection reconciliation
|
|
1584
1706
|
socket.on('acp:reconcile', async (data: { activeSessionIds: string[]; pendingKillSessionIds: string[] }) => {
|
|
1585
1707
|
if (!data || !isStringArray(data.activeSessionIds) || !isStringArray(data.pendingKillSessionIds)) {
|