agent-gear 2.20.0 → 2.22.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 +37 -9
- package/dist/cloud-host/supervisor.d.ts.map +1 -1
- package/dist/cloud-host/supervisor.js +6 -1
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +35 -0
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/commands/github.d.ts +17 -0
- package/dist/commands/github.d.ts.map +1 -0
- package/dist/commands/github.js +24 -0
- package/dist/commands/github.js.map +1 -0
- package/dist/commands/github.test.d.ts +2 -0
- package/dist/commands/github.test.d.ts.map +1 -0
- package/dist/commands/github.test.js +31 -0
- package/dist/commands/github.test.js.map +1 -0
- package/dist/github/credential-manager.d.ts +11 -3
- package/dist/github/credential-manager.d.ts.map +1 -1
- package/dist/github/credential-manager.js +65 -19
- package/dist/github/credential-manager.js.map +1 -1
- package/dist/github/credential-manager.test.js +89 -0
- package/dist/github/credential-manager.test.js.map +1 -1
- package/dist/github/gh-config.d.ts +6 -0
- package/dist/github/gh-config.d.ts.map +1 -1
- package/dist/github/gh-config.js +16 -4
- package/dist/github/gh-config.js.map +1 -1
- package/dist/github/gh-config.test.js +25 -0
- package/dist/github/gh-config.test.js.map +1 -1
- package/dist/github/project-token.d.ts +81 -0
- package/dist/github/project-token.d.ts.map +1 -0
- package/dist/github/project-token.js +232 -0
- package/dist/github/project-token.js.map +1 -0
- package/dist/github/project-token.test.d.ts +2 -0
- package/dist/github/project-token.test.d.ts.map +1 -0
- package/dist/github/project-token.test.js +256 -0
- package/dist/github/project-token.test.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.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/workload-executor.d.ts +5 -0
- package/dist/workload-executor.d.ts.map +1 -1
- package/dist/workload-executor.js.map +1 -1
- package/dist/workload-executor.test.js +34 -0
- package/dist/workload-executor.test.js.map +1 -1
- package/package.json +1 -1
- package/src/cloud-host/supervisor.test.ts +43 -0
- package/src/cloud-host/supervisor.ts +5 -1
- package/src/commands/github.test.ts +38 -0
- package/src/commands/github.ts +36 -0
- package/src/github/credential-manager.test.ts +113 -0
- package/src/github/credential-manager.ts +68 -17
- package/src/github/gh-config.test.ts +25 -0
- package/src/github/gh-config.ts +17 -4
- package/src/github/project-token.test.ts +314 -0
- package/src/github/project-token.ts +292 -0
- package/src/index.ts +4 -0
- package/src/knowledge-bases.test.ts +93 -65
- package/src/knowledge-bases.ts +43 -78
- package/src/workload-executor.test.ts +48 -0
- package/src/workload-executor.ts +5 -0
|
@@ -5,10 +5,8 @@ import { tmpdir } from 'node:os';
|
|
|
5
5
|
import {
|
|
6
6
|
scanKnowledgeBases,
|
|
7
7
|
REPO_KNOWLEDGE_BASE_SOURCES,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
MAX_SCHEMA_BYTES,
|
|
11
|
-
MAX_DOCUMENT_BYTES,
|
|
8
|
+
MAX_ENTRIES_PER_FOLDER,
|
|
9
|
+
MAX_FILE_BYTES,
|
|
12
10
|
} from './knowledge-bases.js';
|
|
13
11
|
|
|
14
12
|
let root: string;
|
|
@@ -124,86 +122,116 @@ describe('scanKnowledgeBases', () => {
|
|
|
124
122
|
}
|
|
125
123
|
});
|
|
126
124
|
|
|
127
|
-
it('T-RKB-SCAN7
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
it('T-RKB-SCAN7 (deprecated) 上限矩阵已由 T-RKB2-SCAN1~5 取代', () => {
|
|
126
|
+
expect(MAX_ENTRIES_PER_FOLDER).toBe(1000);
|
|
127
|
+
expect(MAX_FILE_BYTES).toBe(1024 * 1024);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('T-RKB2-SCAN1 单库 >50 篇文档且累计 >1MiB:全部加载、不截断', async () => {
|
|
131
|
+
const kb = await makeKb(zagDir, 'many');
|
|
132
|
+
const chunk = 'x'.repeat(20 * 1024);
|
|
133
|
+
for (let i = 0; i < 60; i += 1) {
|
|
134
|
+
await writeFile(join(kb, `d${String(i).padStart(2, '0')}.md`), chunk);
|
|
131
135
|
}
|
|
136
|
+
|
|
132
137
|
const result = await scanKnowledgeBases(root);
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
expect(
|
|
136
|
-
expect(
|
|
137
|
-
expect(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const bigSchemaRoot = await mkdtemp(join(tmpdir(), 'rkb-schema-'));
|
|
141
|
-
const bigSchemaDir = join(bigSchemaRoot, '.zag', 'knowledges');
|
|
142
|
-
const bigKb = join(bigSchemaDir, 'big');
|
|
143
|
-
await mkdir(bigKb, { recursive: true });
|
|
144
|
-
await writeFile(join(bigKb, 'knowledge-base.json'), 'x'.repeat(MAX_SCHEMA_BYTES + 1));
|
|
145
|
-
const bigSchemaResult = await scanKnowledgeBases(bigSchemaRoot);
|
|
146
|
-
expect(bigSchemaResult.sources[0].knowledgeBases[0]).toEqual({
|
|
147
|
-
folder: 'big',
|
|
148
|
-
error: 'file too large',
|
|
149
|
-
});
|
|
150
|
-
await rm(bigSchemaRoot, { recursive: true, force: true });
|
|
151
|
-
|
|
152
|
-
// 文档数 >50 截断;单文档 >128KiB 该文档 error;单库 >1MiB 停止并 truncated
|
|
153
|
-
const limitsRoot = await mkdtemp(join(tmpdir(), 'rkb-limits-'));
|
|
154
|
-
const limitsDir = join(limitsRoot, '.zag', 'knowledges');
|
|
155
|
-
const manyDir = join(limitsDir, 'many');
|
|
156
|
-
await mkdir(manyDir, { recursive: true });
|
|
157
|
-
await writeFile(join(manyDir, 'knowledge-base.json'), '{"name":"many"}');
|
|
158
|
-
for (let i = 0; i < MAX_DOCUMENTS_PER_BASE + 2; i += 1) {
|
|
159
|
-
await writeFile(join(manyDir, `d${String(i).padStart(2, '0')}.md`), '# x');
|
|
160
|
-
}
|
|
161
|
-
const bigDocDir = join(limitsDir, 'bigdoc');
|
|
162
|
-
await mkdir(bigDocDir, { recursive: true });
|
|
163
|
-
await writeFile(join(bigDocDir, 'knowledge-base.json'), '{"name":"bigdoc"}');
|
|
164
|
-
await writeFile(join(bigDocDir, 'big.md'), 'x'.repeat(MAX_DOCUMENT_BYTES + 1));
|
|
165
|
-
const limits = await scanKnowledgeBases(limitsRoot);
|
|
166
|
-
const many = limits.sources[0].knowledgeBases.find((item) => item.folder === 'many');
|
|
167
|
-
const bigDoc = limits.sources[0].knowledgeBases.find((item) => item.folder === 'bigdoc');
|
|
168
|
-
expect(many?.truncated).toBe(true);
|
|
169
|
-
expect(many?.documents).toHaveLength(MAX_DOCUMENTS_PER_BASE);
|
|
170
|
-
expect(bigDoc?.documents).toEqual([{ path: 'big.md', error: 'file too large' }]);
|
|
171
|
-
await rm(limitsRoot, { recursive: true, force: true });
|
|
138
|
+
const entry = result.sources[0].knowledgeBases[0];
|
|
139
|
+
|
|
140
|
+
expect(entry.documents).toHaveLength(60);
|
|
141
|
+
expect(entry.documents?.every((doc) => doc.content?.length === chunk.length)).toBe(true);
|
|
142
|
+
expect(entry.truncated).toBe(false);
|
|
143
|
+
expect(result.sources[0].truncated).toBe(false);
|
|
144
|
+
expect(result.truncated).toBe(false);
|
|
172
145
|
});
|
|
173
146
|
|
|
174
|
-
it('T-
|
|
175
|
-
const kb = await makeKb(zagDir, '
|
|
176
|
-
//
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
await writeFile(join(kb, `part${String(i).padStart(2, '0')}.md`), chunk);
|
|
147
|
+
it('T-RKB2-SCAN2 单文件夹 >1000 直接子项:按名称取前 1000、库级 truncated 上浮', async () => {
|
|
148
|
+
const kb = await makeKb(zagDir, 'big');
|
|
149
|
+
// 1000 个文件 + 1 个目录(目录名排序在最后)→ 直接子项 1001,目录层被截断
|
|
150
|
+
for (let i = 0; i < MAX_ENTRIES_PER_FOLDER; i += 1) {
|
|
151
|
+
await writeFile(join(kb, `a${String(i).padStart(4, '0')}.md`), '# x');
|
|
180
152
|
}
|
|
153
|
+
await mkdir(join(kb, 'z-extra'), { recursive: true });
|
|
154
|
+
await writeFile(join(kb, 'z-extra', 'deep.md'), '# deep');
|
|
181
155
|
|
|
182
156
|
const result = await scanKnowledgeBases(root);
|
|
183
157
|
const entry = result.sources[0].knowledgeBases[0];
|
|
158
|
+
expect(entry.documents).toHaveLength(MAX_ENTRIES_PER_FOLDER);
|
|
159
|
+
expect(entry.documents?.some((doc) => doc.path === 'z-extra/deep.md')).toBe(false);
|
|
184
160
|
expect(entry.truncated).toBe(true);
|
|
185
|
-
expect((entry.documents?.length ?? 0)).toBeLessThan(12);
|
|
186
161
|
expect(result.sources[0].truncated).toBe(true);
|
|
162
|
+
expect(result.truncated).toBe(true);
|
|
163
|
+
|
|
164
|
+
// 其余目录不受影响
|
|
165
|
+
const other = await makeKb(zagDir, 'small');
|
|
166
|
+
await writeFile(join(other, 'ok.md'), '# ok');
|
|
167
|
+
const again = await scanKnowledgeBases(root);
|
|
168
|
+
const small = again.sources[0].knowledgeBases.find((item) => item.folder === 'small');
|
|
169
|
+
expect(small?.truncated).toBe(false);
|
|
170
|
+
expect(small?.documents).toHaveLength(1);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('T-RKB2-SCAN3 单文档 >1MiB:该文档 error 不读内容,其余正常且库不截断', async () => {
|
|
174
|
+
const kb = await makeKb(zagDir, 'mixed');
|
|
175
|
+
await writeFile(join(kb, 'big.md'), 'x'.repeat(MAX_FILE_BYTES + 1));
|
|
176
|
+
await writeFile(join(kb, 'ok.md'), '# ok');
|
|
177
|
+
|
|
178
|
+
const result = await scanKnowledgeBases(root);
|
|
179
|
+
const entry = result.sources[0].knowledgeBases[0];
|
|
180
|
+
const big = entry.documents?.find((doc) => doc.path === 'big.md');
|
|
181
|
+
const ok = entry.documents?.find((doc) => doc.path === 'ok.md');
|
|
182
|
+
|
|
183
|
+
expect(big).toEqual({ path: 'big.md', error: 'file too large' });
|
|
184
|
+
expect(ok?.content).toBe('# ok');
|
|
185
|
+
expect(entry.truncated).toBe(false);
|
|
186
|
+
expect(result.truncated).toBe(false);
|
|
187
187
|
});
|
|
188
188
|
|
|
189
|
-
it('T-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
189
|
+
it('T-RKB2-SCAN4 knowledge-base.json >1MiB:整库 file too large 且不读文档', async () => {
|
|
190
|
+
const kbDir = join(zagDir, 'badschema');
|
|
191
|
+
await mkdir(kbDir, { recursive: true });
|
|
192
|
+
await writeFile(join(kbDir, 'knowledge-base.json'), 'x'.repeat(MAX_FILE_BYTES + 1));
|
|
193
|
+
await writeFile(join(kbDir, 'doc.md'), '# doc');
|
|
194
|
+
|
|
195
|
+
const result = await scanKnowledgeBases(root);
|
|
196
|
+
|
|
197
|
+
expect(result.sources[0].knowledgeBases[0]).toEqual({
|
|
198
|
+
folder: 'badschema',
|
|
199
|
+
error: 'file too large',
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('T-RKB2-SCAN5 来源目录 >1000 子文件夹:前 1000 识别、仅来源级截断', async () => {
|
|
204
|
+
for (let i = 0; i <= MAX_ENTRIES_PER_FOLDER; i += 1) {
|
|
205
|
+
await makeKb(zagDir, `kb${String(i).padStart(4, '0')}`);
|
|
196
206
|
}
|
|
197
207
|
|
|
198
208
|
const result = await scanKnowledgeBases(root);
|
|
199
209
|
const source = result.sources[0];
|
|
200
|
-
const third = source.knowledgeBases.find((item) => item.folder === 'source-limit-2');
|
|
201
210
|
|
|
211
|
+
expect(source.knowledgeBases).toHaveLength(MAX_ENTRIES_PER_FOLDER);
|
|
212
|
+
expect(source.knowledgeBases[0].folder).toBe('kb0000');
|
|
213
|
+
expect(source.knowledgeBases.at(-1)?.folder).toBe('kb0999');
|
|
214
|
+
expect(source.truncated).toBe(true);
|
|
202
215
|
expect(result.truncated).toBe(true);
|
|
216
|
+
// 来源目录层截断不绑定到具体知识库
|
|
217
|
+
expect(source.knowledgeBases.every((item) => item.truncated !== true)).toBe(true);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('T-RKB2-SCAN5b 来源目录「目录 + 散置文件」合计 >1000:按名称合并取前 1000,窗口外文件夹不识别', async () => {
|
|
221
|
+
// 999 个散置文件 + 2 个知识库文件夹(名称排序在文件之后)→ 直接子项 1001
|
|
222
|
+
for (let i = 0; i < MAX_ENTRIES_PER_FOLDER - 1; i += 1) {
|
|
223
|
+
await writeFile(join(zagDir, `a${String(i).padStart(4, '0')}.md`), '# note');
|
|
224
|
+
}
|
|
225
|
+
await makeKb(zagDir, 'z-kb-in');
|
|
226
|
+
await makeKb(zagDir, 'z-kb-out');
|
|
227
|
+
|
|
228
|
+
const result = await scanKnowledgeBases(root);
|
|
229
|
+
const source = result.sources[0];
|
|
230
|
+
|
|
231
|
+
// 窗口内仅容纳 999 个文件 + z-kb-in;z-kb-out 在窗口外不遍历、不识别
|
|
232
|
+
expect(source.knowledgeBases.map((item) => item.folder)).toEqual(['z-kb-in']);
|
|
203
233
|
expect(source.truncated).toBe(true);
|
|
204
|
-
|
|
205
|
-
expect(third?.documents?.length ?? 0).toBeLessThan(8);
|
|
206
|
-
expect(third?.truncated).toBe(true);
|
|
234
|
+
expect(result.truncated).toBe(true);
|
|
207
235
|
});
|
|
208
236
|
|
|
209
237
|
it('T-RKB-SCAN8 符号链接:指向常规 .md 文件读取;指向目录/悬空跳过', async () => {
|
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
|
}
|
|
@@ -818,3 +818,51 @@ describe('WorkloadExecutor githubAuth prefetch (T-WE-G)', () => {
|
|
|
818
818
|
expect(results[0].log).not.toContain(secret);
|
|
819
819
|
});
|
|
820
820
|
});
|
|
821
|
+
|
|
822
|
+
describe('WorkloadExecutor githubRepoFullName passthrough (T-WE-G5)', () => {
|
|
823
|
+
it('T-WE-G5: passes githubRepoFullName to the hook payload', async () => {
|
|
824
|
+
const executor = new WorkloadExecutor();
|
|
825
|
+
const { results, sink } = collect();
|
|
826
|
+
const seen: Array<string | undefined> = [];
|
|
827
|
+
|
|
828
|
+
await executor.runBatch(
|
|
829
|
+
{
|
|
830
|
+
phase: 'init',
|
|
831
|
+
githubAuth: true,
|
|
832
|
+
githubRepoFullName: 'owner/repo',
|
|
833
|
+
items: [{ logId: 'g1', workloadName: 'One', script: 'echo ok', timeoutMs: 5000 }],
|
|
834
|
+
},
|
|
835
|
+
sink,
|
|
836
|
+
{
|
|
837
|
+
beforeGithubAuth: async (payload) => {
|
|
838
|
+
seen.push(payload.githubRepoFullName);
|
|
839
|
+
},
|
|
840
|
+
},
|
|
841
|
+
);
|
|
842
|
+
|
|
843
|
+
expect(seen).toEqual(['owner/repo']);
|
|
844
|
+
expect(results.map((result) => result.status)).toEqual(['success']);
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
it('T-WE-G6: absent githubRepoFullName keeps the legacy payload', async () => {
|
|
848
|
+
const executor = new WorkloadExecutor();
|
|
849
|
+
const { sink } = collect();
|
|
850
|
+
const seen: Array<string | undefined> = [];
|
|
851
|
+
|
|
852
|
+
await executor.runBatch(
|
|
853
|
+
{
|
|
854
|
+
phase: 'init',
|
|
855
|
+
githubAuth: true,
|
|
856
|
+
items: [{ logId: 'g1', workloadName: 'One', script: 'echo ok', timeoutMs: 5000 }],
|
|
857
|
+
},
|
|
858
|
+
sink,
|
|
859
|
+
{
|
|
860
|
+
beforeGithubAuth: async (payload) => {
|
|
861
|
+
seen.push(payload.githubRepoFullName);
|
|
862
|
+
},
|
|
863
|
+
},
|
|
864
|
+
);
|
|
865
|
+
|
|
866
|
+
expect(seen).toEqual([undefined]);
|
|
867
|
+
});
|
|
868
|
+
});
|
package/src/workload-executor.ts
CHANGED
|
@@ -49,6 +49,11 @@ export interface WorkloadRunPayload {
|
|
|
49
49
|
* 缺省/false 行为与旧版完全一致。
|
|
50
50
|
*/
|
|
51
51
|
githubAuth?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* 本批针对的仓库 `owner/repo`(仅 `githubAuth === true` 时有意义)。
|
|
54
|
+
* 提供时客户端按该项目预取凭据;缺省保持主项目语义。
|
|
55
|
+
*/
|
|
56
|
+
githubRepoFullName?: string;
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
/** `runBatch` 的可选行为注入(默认空实现,保证旧调用方零变化)。 */
|