-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathskill_registry_test.cpp
More file actions
401 lines (337 loc) · 14 KB
/
Copy pathskill_registry_test.cpp
File metadata and controls
401 lines (337 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "skills/skill_registry.hpp"
#include "agent_loop.hpp"
#include "commands/command_registry.hpp"
#include "config/config.hpp"
#include "permissions.hpp"
#include "tool/tool_executor.hpp"
#include "utils/encoding.hpp"
#include "utils/utf8_path.hpp"
#include <gtest/gtest.h>
#include <atomic>
#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>
#include <unordered_set>
namespace fs = std::filesystem;
namespace {
void write_skill(const fs::path& root,
const std::string& category,
const std::string& name,
const std::string& description) {
fs::path dir = root / acecode::path_from_utf8(category) / acecode::path_from_utf8(name);
fs::create_directories(dir);
std::ofstream ofs(dir / "SKILL.md", std::ios::binary);
ofs << "---\n"
<< "name: " << name << "\n"
<< "description: " << description << "\n"
<< "---\n\n"
<< "# " << name << "\n\n"
<< description << "\n";
}
class NoopProvider : public acecode::LlmProvider {
public:
acecode::ChatResponse chat(const std::vector<acecode::ChatMessage>&,
const std::vector<acecode::ToolDef>&) override {
acecode::ChatResponse response;
response.finish_reason = "stop";
return response;
}
void chat_stream(const std::vector<acecode::ChatMessage>&,
const std::vector<acecode::ToolDef>&,
const acecode::StreamCallback& callback,
std::atomic<bool>* = nullptr) override {
acecode::StreamEvent done;
done.type = acecode::StreamEventType::Done;
callback(done);
}
std::string name() const override { return "noop"; }
bool is_authenticated() override { return true; }
std::string model() const override { return "noop"; }
void set_model(const std::string&) override {}
};
void append_utf16le(std::string& out, char16_t ch) {
out.push_back(static_cast<char>(ch & 0xFF));
out.push_back(static_cast<char>((ch >> 8) & 0xFF));
}
std::string utf16le_bom_bytes(std::u16string_view text) {
std::string out;
out.push_back(static_cast<char>(0xFF));
out.push_back(static_cast<char>(0xFE));
for (char16_t ch : text) append_utf16le(out, ch);
return out;
}
class SkillRegistryCompatTest : public ::testing::Test {
protected:
fs::path temp_root;
void SetUp() override {
temp_root = fs::temp_directory_path() / fs::path("acecode-skill-registry-test");
std::error_code ec;
fs::remove_all(temp_root, ec);
fs::create_directories(temp_root);
}
void TearDown() override {
std::error_code ec;
fs::remove_all(temp_root, ec);
}
};
TEST_F(SkillRegistryCompatTest, LoadsSkillFromAgentRoot) {
fs::path acecode_root = temp_root / "acecode-root";
fs::path agent_root = temp_root / "agent-root";
write_skill(agent_root, "engineering", "agent-only", "loaded from .agent root");
acecode::SkillRegistry registry;
registry.set_scan_roots({acecode_root, agent_root});
registry.scan();
auto found = registry.find("agent-only");
ASSERT_TRUE(found.has_value());
EXPECT_EQ(found->name, "agent-only");
EXPECT_EQ(found->description, "loaded from .agent root");
}
TEST_F(SkillRegistryCompatTest, PrefersEarlierRootForDuplicateSkillNames) {
fs::path acecode_root = temp_root / "acecode-root";
fs::path agent_root = temp_root / "agent-root";
write_skill(acecode_root, "engineering", "shared-skill", "from acecode root");
write_skill(agent_root, "engineering", "shared-skill", "from agent root");
acecode::SkillRegistry registry;
registry.set_scan_roots({acecode_root, agent_root});
registry.scan();
auto found = registry.find("shared-skill");
ASSERT_TRUE(found.has_value());
EXPECT_EQ(found->description, "from acecode root");
EXPECT_EQ(registry.list().size(), 1u);
}
TEST_F(SkillRegistryCompatTest, MissingAllowlistKeepsUnrestrictedDiscovery) {
fs::path root = temp_root / "skills-root";
write_skill(root, "engineering", "alpha", "first");
write_skill(root, "engineering", "beta", "second");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.set_allowed(std::nullopt);
registry.scan();
const auto skills = registry.list();
ASSERT_EQ(skills.size(), 2u);
EXPECT_EQ(skills[0].name, "alpha");
EXPECT_EQ(skills[1].name, "beta");
}
TEST_F(SkillRegistryCompatTest, EngagedEmptyAllowlistHidesEverySkill) {
fs::path root = temp_root / "skills-root";
write_skill(root, "engineering", "alpha", "first");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.set_allowed(std::unordered_set<std::string>{});
registry.scan();
EXPECT_TRUE(registry.list().empty());
EXPECT_FALSE(registry.find("alpha").has_value());
}
TEST_F(SkillRegistryCompatTest, AllowlistSelectsOnlyNamedSkillsAcrossRefreshes) {
fs::path root = temp_root / "skills-root";
write_skill(root, "engineering", "alpha", "first");
write_skill(root, "engineering", "beta", "second");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.set_allowed(std::unordered_set<std::string>{"beta"});
registry.scan();
ASSERT_EQ(registry.list().size(), 1u);
EXPECT_EQ(registry.list()[0].name, "beta");
write_skill(root, "engineering", "gamma", "created later");
const auto refreshed = registry.list();
ASSERT_EQ(refreshed.size(), 1u);
EXPECT_EQ(refreshed[0].name, "beta");
}
TEST_F(SkillRegistryCompatTest, DisabledNamesTakePrecedenceOverAllowlist) {
fs::path root = temp_root / "skills-root";
write_skill(root, "engineering", "alpha", "first");
write_skill(root, "engineering", "beta", "second");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.set_disabled(std::unordered_set<std::string>{"alpha"});
registry.set_allowed(std::unordered_set<std::string>{"alpha", "beta"});
registry.scan();
const auto skills = registry.list();
ASSERT_EQ(skills.size(), 1u);
EXPECT_EQ(skills[0].name, "beta");
EXPECT_FALSE(registry.find("alpha").has_value());
}
TEST_F(SkillRegistryCompatTest, PreservesUtf8CategoryDescriptionAndBody) {
fs::path root = temp_root / "skills-root";
write_skill(root, u8"中文分类", "unicode-skill", u8"处理中文描述");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.scan();
auto found = registry.find("unicode-skill");
ASSERT_TRUE(found.has_value());
EXPECT_EQ(found->category, u8"中文分类");
EXPECT_EQ(found->description, u8"处理中文描述");
EXPECT_TRUE(acecode::is_valid_utf8(found->category));
EXPECT_TRUE(acecode::is_valid_utf8(found->description));
std::string body = registry.read_skill_body("unicode-skill");
EXPECT_NE(body.find(u8"处理中文描述"), std::string::npos);
EXPECT_TRUE(acecode::is_valid_utf8(body));
}
TEST_F(SkillRegistryCompatTest, LargeUtf8SkillNotGarbledWhenReadBudgetSplitsCharacter) {
// 触发场景:像 paoffice-im 这类 SKILL.md 把整套 CLI 文档写进 body,文件远超
// frontmatter 8KB 读取预算。旧实现在第 8192 字节硬切,常把一个 3 字节中文字符
// 切成两半 → ensure_utf8 判整块非法 UTF-8 → 中文 Windows 上按 GBK 重解码 →
// 顶部 frontmatter(含 description)从第一个字节起整串乱码。
// 期望:description 原样保留、仍是合法 UTF-8。
// 回归 bug 表现:WebUI 斜杠命令列表里该 skill 的描述显示为乱码,其余 skill 正常。
fs::path root = temp_root / "skills-root";
fs::path dir = root / "general" / "big-skill";
fs::create_directories(dir);
const std::string description = u8"企业 IM 聊天 CLI 工具";
const std::string header =
"---\n"
"name: big-skill\n"
"description: " + description + "\n"
"---\n\n";
// 用 3 字节中文字符 "国"(E5 9B BD)铺满 body,凑到远超 8192 字节。
std::string cjk_run;
while (cjk_run.size() < 9000) cjk_run += u8"国";
// 关键:确定性地让第 8192 字节(首个未读字节)落在续字节上,即读取窗口
// [0,8191] 必然以残缺字符结尾——这正是旧 bug 的触发条件。用 ASCII 垫片微调对齐。
std::string pad;
auto build = [&] { return header + "# big-skill " + pad + "\n\n" + cjk_run; };
std::string file = build();
ASSERT_GT(file.size(), 8192u);
auto is_continuation = [](unsigned char b) { return (b & 0xC0) == 0x80; };
while (!is_continuation(static_cast<unsigned char>(file[8192]))) {
pad += "x";
file = build();
}
std::ofstream ofs(dir / "SKILL.md", std::ios::binary);
ofs.write(file.data(), static_cast<std::streamsize>(file.size()));
ofs.close();
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.scan();
auto found = registry.find("big-skill");
ASSERT_TRUE(found.has_value());
EXPECT_EQ(found->description, description);
EXPECT_TRUE(acecode::is_valid_utf8(found->description));
}
TEST_F(SkillRegistryCompatTest, LoadsUtf16LegacyHeaderSkill) {
fs::path root = temp_root / "skills-root";
fs::path dir = root / "general" / "calculator";
fs::create_directories(dir);
std::ofstream ofs(dir / "SKILL.md", std::ios::binary);
ofs << utf16le_bom_bytes(
u"name: calculator\r\n"
u"description: A simple calculator skill\r\n"
u"category: general\r\n"
u"\r\n"
u"---\r\n"
u"\r\n"
u"# Calculator Skill\r\n");
ofs.close();
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.scan();
auto found = registry.find("calculator");
ASSERT_TRUE(found.has_value());
EXPECT_EQ(found->name, "calculator");
EXPECT_EQ(found->description, "A simple calculator skill");
EXPECT_EQ(found->category, "general");
EXPECT_TRUE(acecode::is_valid_utf8(found->description));
std::string body = registry.read_skill_body("calculator");
EXPECT_NE(body.find("# Calculator Skill"), std::string::npos);
EXPECT_TRUE(acecode::is_valid_utf8(body));
}
TEST_F(SkillRegistryCompatTest, ListSeesSkillAddedAfterInitialScanWithoutReload) {
fs::path root = temp_root / "skills-root";
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.scan();
EXPECT_TRUE(registry.list().empty());
write_skill(root, "engineering", "fresh-skill", "created after scan");
auto all = registry.list();
ASSERT_EQ(all.size(), 1u);
EXPECT_EQ(all[0].name, "fresh-skill");
EXPECT_EQ(all[0].description, "created after scan");
}
TEST_F(SkillRegistryCompatTest, FindSeesUpdatedMetadataWithoutReload) {
fs::path root = temp_root / "skills-root";
write_skill(root, "engineering", "mutable-skill", "first description");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.scan();
ASSERT_EQ(registry.find("mutable-skill")->description, "first description");
write_skill(root, "engineering", "mutable-skill", "updated description");
auto updated = registry.find("mutable-skill");
ASSERT_TRUE(updated.has_value());
EXPECT_EQ(updated->description, "updated description");
}
TEST_F(SkillRegistryCompatTest, SupportingFilesAreDiscoveredWithoutReload) {
fs::path root = temp_root / "skills-root";
write_skill(root, "engineering", "support-skill", "has support");
acecode::SkillRegistry registry;
registry.set_scan_roots({root});
registry.scan();
EXPECT_TRUE(registry.list_supporting_files("support-skill").empty());
fs::path references = root / "engineering" / "support-skill" / "references";
fs::create_directories(references);
std::ofstream ofs(references / "api.md", std::ios::binary);
ofs << "live reference";
ofs.close();
auto files = registry.list_supporting_files("support-skill");
ASSERT_EQ(files.size(), 1u);
EXPECT_EQ(files[0], "references/api.md");
auto resolved = registry.resolve_skill_file("support-skill", "references/api.md");
ASSERT_TRUE(resolved.has_value());
EXPECT_TRUE(fs::exists(*resolved));
}
TEST_F(SkillRegistryCompatTest, CommandDispatchInvokesNewSkillWithoutReloadingCommands) {
fs::path root = temp_root / "skills-root";
acecode::SkillRegistry skill_registry;
skill_registry.set_scan_roots({root});
skill_registry.scan();
acecode::CommandRegistry command_registry;
acecode::TuiState state;
state.is_waiting = true;
acecode::ToolExecutor tools;
acecode::PermissionManager permissions;
acecode::AgentCallbacks callbacks;
auto provider = std::make_shared<NoopProvider>();
acecode::AgentLoop loop(
[&]() -> std::shared_ptr<acecode::LlmProvider> { return provider; },
tools,
callbacks,
temp_root.string(),
permissions);
acecode::AppConfig config;
acecode::TokenTracker token_tracker;
acecode::CommandContext ctx{
state,
loop,
nullptr,
config,
token_tracker,
permissions,
[] {},
nullptr,
[] {},
nullptr,
&tools,
&skill_registry,
nullptr,
&command_registry,
temp_root.string(),
};
std::vector<std::string> observed_commands;
ctx.on_command_recognized =
[&observed_commands](const std::string& name) {
observed_commands.push_back(name);
};
write_skill(root, "engineering", "fresh-skill", "created after command registration");
EXPECT_TRUE(command_registry.dispatch("/fresh-skill run this", ctx));
EXPECT_EQ(observed_commands,
(std::vector<std::string>{"fresh-skill"}));
ASSERT_EQ(state.conversation.size(), 1u);
EXPECT_NE(state.conversation[0].content.find("[Invoking skill: fresh-skill]"),
std::string::npos);
ASSERT_EQ(state.pending_queue.size(), 1u);
EXPECT_NE(state.pending_queue[0].find("[$fresh-skill]("),
std::string::npos);
EXPECT_NE(state.pending_queue[0].find("SKILL.md)"), std::string::npos);
EXPECT_NE(state.pending_queue[0].find("run this"), std::string::npos);
}
} // namespace