@bolloon/bolloon-agent 0.2.7 → 0.2.8
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/dist/agents/agent-manifest-protocol.js +52 -0
- package/dist/agents/peer-manifest-loader.js +210 -0
- package/dist/agents/pi-sdk.js +24 -41
- package/dist/agents/session-store.js +17 -3
- package/dist/agents/shell-guard.js +2 -2
- package/dist/agents/workflow-pivot-loop.js +12 -3
- package/dist/bootstrap/chat-archiver.js +276 -0
- package/dist/bootstrap/context-collector.js +6 -3
- package/dist/bootstrap/lifecycle-hooks.js +15 -1
- package/dist/bootstrap/memory-compressor.js +170 -0
- package/dist/bootstrap/persona-loader.js +94 -0
- package/dist/network/p2p-outbox.js +161 -0
- package/dist/network/peer-fs.js +420 -0
- package/dist/network/peer-resource-bridge.js +216 -0
- package/dist/web/client.js +29 -27
- package/dist/web/components/p2p/index.js +17 -5
- package/dist/web/components/p2p/p2p-modal.js +9 -2
- package/dist/web/server.js +660 -20
- package/dist/web/util/safe-name.js +32 -0
- package/package.json +1 -1
|
@@ -44,6 +44,16 @@ const localManifest = {
|
|
|
44
44
|
};
|
|
45
45
|
export function setLocalManifest(m) {
|
|
46
46
|
Object.assign(localManifest, m, { publishedAt: Date.now() });
|
|
47
|
+
// 2026-07-05: 显式重置 v2 数组字段 — Partial merge 不会清空未提供的 key,
|
|
48
|
+
// 但调用者通常期望"整片替换", 所以这里强制对齐 (避免测试间泄漏).
|
|
49
|
+
if (!('groups' in m))
|
|
50
|
+
localManifest.groups = [];
|
|
51
|
+
if (!('functions' in m))
|
|
52
|
+
localManifest.functions = [];
|
|
53
|
+
if (!('exportments' in m))
|
|
54
|
+
localManifest.exportments = [];
|
|
55
|
+
if (!('sciences' in m))
|
|
56
|
+
localManifest.sciences = [];
|
|
47
57
|
return localManifest;
|
|
48
58
|
}
|
|
49
59
|
export function getLocalManifest() {
|
|
@@ -58,6 +68,48 @@ export function addLocalAgent(agent) {
|
|
|
58
68
|
localManifest.publishedAt = Date.now();
|
|
59
69
|
return localManifest;
|
|
60
70
|
}
|
|
71
|
+
// ============== 2026-07-05: 4 类资源 setter (groups/function/exportment/science) ==============
|
|
72
|
+
// 默认 localManifest 不带这些字段; 一旦调用就 patch 进去. 不强制覆盖旧值.
|
|
73
|
+
export function addLocalGroup(g) {
|
|
74
|
+
localManifest.groups = localManifest.groups || [];
|
|
75
|
+
const idx = localManifest.groups.findIndex((x) => x.id === g.id);
|
|
76
|
+
if (idx >= 0)
|
|
77
|
+
localManifest.groups[idx] = { ...localManifest.groups[idx], ...g };
|
|
78
|
+
else
|
|
79
|
+
localManifest.groups.push(g);
|
|
80
|
+
localManifest.publishedAt = Date.now();
|
|
81
|
+
return localManifest;
|
|
82
|
+
}
|
|
83
|
+
export function addLocalFunction(f) {
|
|
84
|
+
localManifest.functions = localManifest.functions || [];
|
|
85
|
+
const idx = localManifest.functions.findIndex((x) => x.capability === f.capability);
|
|
86
|
+
if (idx >= 0)
|
|
87
|
+
localManifest.functions[idx] = { ...localManifest.functions[idx], ...f };
|
|
88
|
+
else
|
|
89
|
+
localManifest.functions.push(f);
|
|
90
|
+
localManifest.publishedAt = Date.now();
|
|
91
|
+
return localManifest;
|
|
92
|
+
}
|
|
93
|
+
export function addLocalExportment(e) {
|
|
94
|
+
localManifest.exportments = localManifest.exportments || [];
|
|
95
|
+
const idx = localManifest.exportments.findIndex((x) => x.name === e.name);
|
|
96
|
+
if (idx >= 0)
|
|
97
|
+
localManifest.exportments[idx] = { ...localManifest.exportments[idx], ...e };
|
|
98
|
+
else
|
|
99
|
+
localManifest.exportments.push(e);
|
|
100
|
+
localManifest.publishedAt = Date.now();
|
|
101
|
+
return localManifest;
|
|
102
|
+
}
|
|
103
|
+
export function addLocalScience(s) {
|
|
104
|
+
localManifest.sciences = localManifest.sciences || [];
|
|
105
|
+
const idx = localManifest.sciences.findIndex((x) => x.id === s.id);
|
|
106
|
+
if (idx >= 0)
|
|
107
|
+
localManifest.sciences[idx] = { ...localManifest.sciences[idx], ...s };
|
|
108
|
+
else
|
|
109
|
+
localManifest.sciences.push(s);
|
|
110
|
+
localManifest.publishedAt = Date.now();
|
|
111
|
+
return localManifest;
|
|
112
|
+
}
|
|
61
113
|
// ============== 远端 manifest 缓存 ==============
|
|
62
114
|
const remoteManifests = new Map(); // key = ownerPublicKey
|
|
63
115
|
export function cacheRemoteManifest(m) {
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* peer-manifest-loader.ts — 智能体相互了解的懒加载触发器
|
|
3
|
+
*
|
|
4
|
+
* 设计目的 (2026-07-05):
|
|
5
|
+
* 远程 P2P 节点的 manifest 不默认进 LLM prompt, 只在需要时拉取.
|
|
6
|
+
* 3 个触发点:
|
|
7
|
+
* ① @-mention 远端 channel → 立即拉对方 manifest + 对应 agent 详细描述
|
|
8
|
+
* ② 本地 agent 连续失败 → 兜底拉对方 manifest (对方可能有解)
|
|
9
|
+
* ③ 关键词触发 → 用户说 "持续协助" / "cooperate" 时加载
|
|
10
|
+
*
|
|
11
|
+
* 加载结果拼成 prompt 附加块 (≤2000 字符), 拼到 system prompt 尾部.
|
|
12
|
+
* 不入持久化 prompt, 不污染主对话.
|
|
13
|
+
*/
|
|
14
|
+
import * as fs from 'fs/promises';
|
|
15
|
+
import * as peerFs from '../network/peer-fs.js';
|
|
16
|
+
// ============== 主入口 ==============
|
|
17
|
+
/**
|
|
18
|
+
* 加载对方的 manifest + (可选) 详细 agent 描述, 拼成 prompt 块.
|
|
19
|
+
* 行为:
|
|
20
|
+
* 1) 先看本地 ~/.bolloon/peers/<pk>/capability-index.md 缓存 — 命中且新 (≤1h) → 直接用
|
|
21
|
+
* 2) 否则调 RPC 拉 manifest → 落盘 → 读 capability-index.md
|
|
22
|
+
* 3) 如果 triggerValue 是 agent id 或 capability 名, 再发 RPC 拉详细描述
|
|
23
|
+
*/
|
|
24
|
+
export async function loadPeerManifest(ctx, opts) {
|
|
25
|
+
const t0 = Date.now();
|
|
26
|
+
const pk = ctx.remotePublicKey;
|
|
27
|
+
if (!pk || pk === opts.p2p.getPublicKey())
|
|
28
|
+
return null; // 自己
|
|
29
|
+
let rpcTriggered = false;
|
|
30
|
+
let idx = await peerFs.readPeerIndex(pk);
|
|
31
|
+
let capabilityIndex = await peerFs.readCapabilityIndex(pk);
|
|
32
|
+
// 缓存判断: capability-index 存在 + 索引在 TTL 内 → 跳过 RPC
|
|
33
|
+
const ttl = opts.cacheTtlMs ?? 60 * 60 * 1000;
|
|
34
|
+
if (!idx || !capabilityIndex || (idx.updatedAt && (Date.now() - new Date(idx.updatedAt).getTime()) > ttl)) {
|
|
35
|
+
// 缓存过期或不完整, 发 RPC 拉新
|
|
36
|
+
const since = idx?.manifestTs || 0;
|
|
37
|
+
const req = JSON.stringify({
|
|
38
|
+
v: 3, op: 'agent.manifest.exchange',
|
|
39
|
+
payload: { since, fromPublicKey: opts.p2p.getPublicKey() }
|
|
40
|
+
});
|
|
41
|
+
const r = await opts.p2p.sendToWithWait(pk, req, 3000);
|
|
42
|
+
if (r === 'SENT') {
|
|
43
|
+
rpcTriggered = true;
|
|
44
|
+
// 等异步处理落盘 — 这里 sleep 短时间, 不阻塞主线程太久
|
|
45
|
+
await new Promise(r => setTimeout(r, 500));
|
|
46
|
+
idx = await peerFs.readPeerIndex(pk);
|
|
47
|
+
capabilityIndex = await peerFs.readCapabilityIndex(pk);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (!idx) {
|
|
51
|
+
return {
|
|
52
|
+
publicKey: pk,
|
|
53
|
+
capabilityIndex: '',
|
|
54
|
+
promptBlock: `[远端 peer ${pk.slice(0, 12)}…] 暂无 manifest 缓存 (对方未响应或本地首次连接).`,
|
|
55
|
+
agentDescriptions: [],
|
|
56
|
+
rpcTriggered,
|
|
57
|
+
durationMs: Date.now() - t0,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
// 按 triggerValue 决定要不要拉详细 agent 描述
|
|
61
|
+
const agentDescriptions = [];
|
|
62
|
+
const lowerVal = ctx.triggerValue.toLowerCase();
|
|
63
|
+
// 1) 如果 triggerValue 看起来是 agent id (有特殊前缀/格式) → 直接拉
|
|
64
|
+
// 2) 如果是 capability 名 (编程/翻译/...) → 在 idx 里找匹配 capability 的 agent → 拉
|
|
65
|
+
const candidates = [];
|
|
66
|
+
if (ctx.triggerValue.match(/^(agent_|agt_|bot_|assist_|task_)/)) {
|
|
67
|
+
candidates.push(ctx.triggerValue);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
for (const a of idx.agents) {
|
|
71
|
+
if (a.capabilities.some(c => c.toLowerCase().includes(lowerVal))) {
|
|
72
|
+
candidates.push(a.id);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// 拉详细描述 (限 3 个, 避免 prompt 爆炸)
|
|
77
|
+
for (const agentId of candidates.slice(0, 3)) {
|
|
78
|
+
const localFile = peerFs.getPeerAgentMdPath(pk, agentId);
|
|
79
|
+
let body = '';
|
|
80
|
+
try {
|
|
81
|
+
body = await fs.readFile(localFile, 'utf-8');
|
|
82
|
+
}
|
|
83
|
+
catch { }
|
|
84
|
+
if (!body) {
|
|
85
|
+
// 本地没缓存, 拉 RPC
|
|
86
|
+
const req = JSON.stringify({
|
|
87
|
+
v: 3, op: 'agent.resource.get',
|
|
88
|
+
payload: { agentId, fromPublicKey: opts.p2p.getPublicKey() }
|
|
89
|
+
});
|
|
90
|
+
const r = await opts.p2p.sendToWithWait(pk, req, 3000);
|
|
91
|
+
if (r === 'SENT') {
|
|
92
|
+
rpcTriggered = true;
|
|
93
|
+
// 异步处理, 等 300ms
|
|
94
|
+
await new Promise(r => setTimeout(r, 300));
|
|
95
|
+
try {
|
|
96
|
+
body = await fs.readFile(localFile, 'utf-8');
|
|
97
|
+
}
|
|
98
|
+
catch { }
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (body)
|
|
102
|
+
agentDescriptions.push({ agentId, body: body.slice(0, 1500) });
|
|
103
|
+
}
|
|
104
|
+
// 拼 prompt 附加块
|
|
105
|
+
const block = buildPromptBlock(ctx, capabilityIndex || '(无索引)', agentDescriptions, idx);
|
|
106
|
+
return {
|
|
107
|
+
publicKey: pk,
|
|
108
|
+
capabilityIndex: capabilityIndex || '',
|
|
109
|
+
promptBlock: block,
|
|
110
|
+
agentDescriptions,
|
|
111
|
+
rpcTriggered,
|
|
112
|
+
durationMs: Date.now() - t0,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 把加载结果拼成可注入 system prompt 的文本块 (≤2000 字)
|
|
117
|
+
*/
|
|
118
|
+
function buildPromptBlock(ctx, capabilityIndex, agentDescriptions, idx) {
|
|
119
|
+
const lines = [];
|
|
120
|
+
lines.push(`[远端 peer 临时上下文] ${ctx.reason} → ${ctx.triggerValue}`);
|
|
121
|
+
lines.push(`远端节点: ${idx.ownerName || idx.publicKey.slice(0, 12)}… (${idx.agents.length} agents)`);
|
|
122
|
+
lines.push('');
|
|
123
|
+
lines.push('## 对方能力索引');
|
|
124
|
+
lines.push(capabilityIndex);
|
|
125
|
+
lines.push('');
|
|
126
|
+
if (agentDescriptions.length > 0) {
|
|
127
|
+
lines.push('## 触发相关的 agent 详细描述');
|
|
128
|
+
for (const a of agentDescriptions) {
|
|
129
|
+
lines.push(`### ${a.agentId}`);
|
|
130
|
+
lines.push(a.body);
|
|
131
|
+
lines.push('');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const text = lines.join('\n').trim();
|
|
135
|
+
return text.length > 2000 ? text.slice(0, 1997) + '…' : text;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 检测 /message 处理后是否需要加载远端 peer manifest.
|
|
139
|
+
* 输入:
|
|
140
|
+
* - text (用户原始输入)
|
|
141
|
+
* - channelId (当前 channel)
|
|
142
|
+
* - remoteChannels (远端 channel 列表 [{ id, name, _ownerPublicKey }])
|
|
143
|
+
* - consecutiveFailures (连续 LLM 失败次数, 可选)
|
|
144
|
+
*/
|
|
145
|
+
export function detectLoadTrigger(opts) {
|
|
146
|
+
// 触发 ①: @-mention 远端 channel
|
|
147
|
+
const mentionRe = /@([一-龥A-Za-z0-9_\-]{1,30})/g;
|
|
148
|
+
for (const m of opts.text.matchAll(mentionRe)) {
|
|
149
|
+
const name = m[1];
|
|
150
|
+
const rc = opts.remoteChannels.find(c => c.name === name);
|
|
151
|
+
if (rc && rc._ownerPublicKey) {
|
|
152
|
+
return {
|
|
153
|
+
shouldLoad: true,
|
|
154
|
+
reason: 'mention-remote',
|
|
155
|
+
remotePublicKey: rc._ownerPublicKey,
|
|
156
|
+
triggerValue: name,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// 触发 ③: 关键词
|
|
161
|
+
const text = opts.text.toLowerCase();
|
|
162
|
+
const keywords = ['持续协助', '一起合作', 'cooperate', '协作', '对方能干', 'remote help', 'find peer'];
|
|
163
|
+
for (const k of keywords) {
|
|
164
|
+
if (text.includes(k.toLowerCase())) {
|
|
165
|
+
// 关键词触发 — 用第一个 known remote channel 的 owner
|
|
166
|
+
if (opts.remoteChannels.length > 0 && opts.remoteChannels[0]._ownerPublicKey) {
|
|
167
|
+
return {
|
|
168
|
+
shouldLoad: true,
|
|
169
|
+
reason: 'cooperate-keyword',
|
|
170
|
+
remotePublicKey: opts.remoteChannels[0]._ownerPublicKey,
|
|
171
|
+
triggerValue: k,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// 触发 ②: 连续失败 (3+ 次)
|
|
177
|
+
const fails = opts.consecutiveFailures || 0;
|
|
178
|
+
if (fails >= 3) {
|
|
179
|
+
// 用最近一次 @ 的远端 channel; 没有则取 remoteChannels[0]
|
|
180
|
+
const lastMention = [...opts.text.matchAll(mentionRe)].pop();
|
|
181
|
+
let rc = opts.remoteChannels[0];
|
|
182
|
+
if (lastMention) {
|
|
183
|
+
const found = opts.remoteChannels.find(c => c.name === lastMention[1]);
|
|
184
|
+
if (found)
|
|
185
|
+
rc = found;
|
|
186
|
+
}
|
|
187
|
+
if (rc && rc._ownerPublicKey) {
|
|
188
|
+
return {
|
|
189
|
+
shouldLoad: true,
|
|
190
|
+
reason: 'consecutive-failure',
|
|
191
|
+
remotePublicKey: rc._ownerPublicKey,
|
|
192
|
+
triggerValue: rc.name,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return { shouldLoad: false };
|
|
197
|
+
}
|
|
198
|
+
// ============== 失败计数器 (内存) ==============
|
|
199
|
+
const failureCounters = new Map();
|
|
200
|
+
export function recordFailure(channelId) {
|
|
201
|
+
const cur = (failureCounters.get(channelId) || 0) + 1;
|
|
202
|
+
failureCounters.set(channelId, cur);
|
|
203
|
+
return cur;
|
|
204
|
+
}
|
|
205
|
+
export function clearFailure(channelId) {
|
|
206
|
+
failureCounters.delete(channelId);
|
|
207
|
+
}
|
|
208
|
+
export function getFailures(channelId) {
|
|
209
|
+
return failureCounters.get(channelId) || 0;
|
|
210
|
+
}
|
package/dist/agents/pi-sdk.js
CHANGED
|
@@ -19,7 +19,7 @@ import { getBranchPrefix, getCooldownMs, checkWritePath } from './shell-guard.js
|
|
|
19
19
|
import { DiscoveredAgentsManager, createSocialHeartbeat } from '../social/heartbeat.js';
|
|
20
20
|
import { getGlobalSharedContext } from '../social/global-shared-context.js';
|
|
21
21
|
import { Session, SkillRegistry, saveSession, loadSession } from '@bolloon/constraint-runtime';
|
|
22
|
-
import { loadSkillsFromPaths, defaultSkillPaths
|
|
22
|
+
import { loadSkillsFromPaths, defaultSkillPaths } from './skill-loader.js';
|
|
23
23
|
// Judgment 注入门 (P0): 在主对话 LLM 调起前自动拼入 Top 3 判断力
|
|
24
24
|
// 失败静默, 不阻塞主对话
|
|
25
25
|
import { injectJudgmentGate, recordJudgmentUsage } from '../pi-ecosystem-judgment/injection-gate.js';
|
|
@@ -411,6 +411,8 @@ class PiAgentSession {
|
|
|
411
411
|
promptStartTime = 0;
|
|
412
412
|
/** 当前 channel id (由 getAgentForChannel / prompt 4 参注入, 供 hook / log 使用) */
|
|
413
413
|
currentChannelId = '';
|
|
414
|
+
/** 2026-07-04: 当前 agentId (server.ts 通过 createAgentSession 选项注入), 供 onSessionStart 加载 persona docs */
|
|
415
|
+
currentAgentId = '';
|
|
414
416
|
/** M2.2 (2026-06-17): 当前轮的用户请求 intent, runReActLoop 拼 systemPrompt 时会读这个 */
|
|
415
417
|
currentIntent = 'chitchat';
|
|
416
418
|
currentIntentHint = '';
|
|
@@ -454,6 +456,8 @@ class PiAgentSession {
|
|
|
454
456
|
this.peerId = config.peerId || 'local';
|
|
455
457
|
this.identity = config.identityDoc || this.createDefaultIdentity();
|
|
456
458
|
this.minimaxAvailable = this.checkMinimax();
|
|
459
|
+
// 2026-07-04: 透传 agentId (server.ts 通过 createAgentSession 选项注入)
|
|
460
|
+
this.currentAgentId = config.agentId || '';
|
|
457
461
|
// 2026-06-30: 持久化层可注入 — 测试传 tmpDir, 业务用默认 ~/.bolloon/sessions/cache/
|
|
458
462
|
this._sessionStore = config.sessionStore ?? defaultSessionStore;
|
|
459
463
|
this.constraintLayer = new ConstraintLayer();
|
|
@@ -597,23 +601,17 @@ class PiAgentSession {
|
|
|
597
601
|
* 2. ~/.bolloon/skills/ 全局用户级
|
|
598
602
|
* 3. <cwd>/.bolloon/skills/ 项目级
|
|
599
603
|
* 4. ~/.boll/skills/ 全局 (兼容 bollharness 旧用户)
|
|
600
|
-
*
|
|
601
|
-
*
|
|
604
|
+
*
|
|
605
|
+
* 2026-07-04: 移除 18 个 bollharness builtin skill (findBolloonBuiltinSkillsPath).
|
|
606
|
+
* 历史遗留: 写 pi-sdk 时为方便演示, 把 bolloon 项目里的 19 个 skill 强制注入到 system prompt.
|
|
607
|
+
* 问题: system prompt 涨到 22K chars, LLM (minimax M3) 在 pivot loop 里反复 think 不输出
|
|
608
|
+
* `<final gen>`, session 落盘拿不到最终回答.
|
|
609
|
+
* 现在: 只让用户放 .bolloon/skills/SKILL.md 才生效, 干净且 project-owned.
|
|
602
610
|
*
|
|
603
611
|
* 静默忽略不存在的目录.
|
|
604
612
|
*/
|
|
605
613
|
loadSkills(paths) {
|
|
606
|
-
|
|
607
|
-
if (paths && paths.length > 0) {
|
|
608
|
-
resolved = paths;
|
|
609
|
-
}
|
|
610
|
-
else {
|
|
611
|
-
resolved = [
|
|
612
|
-
...defaultSkillPaths(os.homedir(), this.cwd),
|
|
613
|
-
// bolloon 仓库内置 skill (相对本 npm 包的位置)
|
|
614
|
-
this.findBolloonBuiltinSkillsPath(),
|
|
615
|
-
].filter((p) => Boolean(p));
|
|
616
|
-
}
|
|
614
|
+
const resolved = (paths && paths.length > 0) ? paths : defaultSkillPaths(os.homedir(), this.cwd);
|
|
617
615
|
loadSkillsFromPaths(resolved)
|
|
618
616
|
.then((skills) => {
|
|
619
617
|
for (const s of skills) {
|
|
@@ -622,36 +620,17 @@ class PiAgentSession {
|
|
|
622
620
|
}
|
|
623
621
|
this.skillRegistry.register(s);
|
|
624
622
|
}
|
|
625
|
-
console.log(`[loadSkills] 已加载 ${skills.length} 个 skill
|
|
623
|
+
console.log(`[loadSkills] 已加载 ${skills.length} 个 skill from ${resolved.join(', ')}`);
|
|
624
|
+
if (skills.length > 0) {
|
|
625
|
+
for (const s of skills) {
|
|
626
|
+
console.log(` - ${s.name}: ${s.description.substring(0, 100)}${s.description.length > 100 ? '...' : ''}`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
626
629
|
})
|
|
627
630
|
.catch((err) => {
|
|
628
631
|
console.error('[loadSkills] 加载失败:', err);
|
|
629
632
|
});
|
|
630
633
|
}
|
|
631
|
-
/**
|
|
632
|
-
* 定位 bolloon 仓库内置的 bollharness skill 目录.
|
|
633
|
-
* 向上回溯 cwd, 找第一个包含 src/bollharness/.boll/skills 的祖先.
|
|
634
|
-
* 找不到时返回 null (例如把 bolloon-agent 作为外部依赖安装时).
|
|
635
|
-
*/
|
|
636
|
-
findBolloonBuiltinSkillsPath() {
|
|
637
|
-
let dir = this.cwd;
|
|
638
|
-
for (let i = 0; i < 6; i++) {
|
|
639
|
-
const candidate = path.join(dir, 'src', 'bollharness', '.boll', 'skills');
|
|
640
|
-
try {
|
|
641
|
-
if (fsSync.existsSync(candidate) && fsSync.statSync(candidate).isDirectory()) {
|
|
642
|
-
return candidate;
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
catch {
|
|
646
|
-
// 忽略 stat 异常, 继续向上
|
|
647
|
-
}
|
|
648
|
-
const parent = path.dirname(dir);
|
|
649
|
-
if (parent === dir)
|
|
650
|
-
break;
|
|
651
|
-
dir = parent;
|
|
652
|
-
}
|
|
653
|
-
return null;
|
|
654
|
-
}
|
|
655
634
|
async initHarness() {
|
|
656
635
|
try {
|
|
657
636
|
const { createBollharnessIntegration } = await import('../bollharness-integration/index.js');
|
|
@@ -1913,9 +1892,13 @@ class PiAgentSession {
|
|
|
1913
1892
|
}
|
|
1914
1893
|
// Bootstrap SessionStart: 收集项目 Context, 拼到 systemAddition 头部
|
|
1915
1894
|
// (失败静默, 5s 限流防止循环)
|
|
1895
|
+
// 2026-07-04: 透传 agentId 让 onSessionStart 加载 persona 文档
|
|
1916
1896
|
let bootstrapAddition = '';
|
|
1917
1897
|
try {
|
|
1918
|
-
const ss = await onSessionStart({
|
|
1898
|
+
const ss = await onSessionStart({
|
|
1899
|
+
channelId: this.currentChannelId || undefined,
|
|
1900
|
+
agentId: this.currentAgentId || undefined,
|
|
1901
|
+
});
|
|
1919
1902
|
bootstrapAddition = ss.systemAddition || '';
|
|
1920
1903
|
}
|
|
1921
1904
|
catch (err) {
|
|
@@ -2144,7 +2127,7 @@ ${this.getToolDefinitions()}
|
|
|
2144
2127
|
// promptWithPivotLoop 路径 0 step events — UI 显示 timeline 但永远是空
|
|
2145
2128
|
// 2026-06-17: 透传 signal 让 abort 工作 — loop.execute() 当前不接 signal 参数,
|
|
2146
2129
|
// 所以 abort 行为通过 this.currentSignal 共享给 loop 内部读 (后续 M3.2 接 task plan 时一起加)
|
|
2147
|
-
const result = await loop.execute(input, llm, systemPrompt, this.currentOnStream ?? undefined);
|
|
2130
|
+
const result = await loop.execute(input, llm, systemPrompt, this.currentOnStream ?? undefined, this.currentSignal ?? undefined);
|
|
2148
2131
|
this.messageHistory.push({ role: 'user', content: input });
|
|
2149
2132
|
if (result.response) {
|
|
2150
2133
|
this.messageHistory.push({ role: 'assistant', content: result.response });
|
|
@@ -28,12 +28,26 @@ export class SessionStore {
|
|
|
28
28
|
get dir() {
|
|
29
29
|
return this.cacheDir;
|
|
30
30
|
}
|
|
31
|
-
/** 单文件路径 — 暴露出来方便测试和外部读取.
|
|
31
|
+
/** 单文件路径 — 暴露出来方便测试和外部读取.
|
|
32
|
+
*
|
|
33
|
+
* 2026-07-04 fix: Windows 文件名禁止 `:` (NTFS). web server 用 `channelId:currentSessionId`
|
|
34
|
+
* 拼 sessionKey (server.ts:1759 之类), 会含 `:`. 在 Linux/macOS 上能用, Windows 上
|
|
35
|
+
* fs.writeFile 抛 EINVAL. 修法: filename 层 escape `:` → `__`, key 保持不变
|
|
36
|
+
* (load/save/listKeys/deleteKey 全部透明).
|
|
37
|
+
*/
|
|
32
38
|
pathFor(key) {
|
|
33
39
|
if (!key || key.includes('/') || key.includes('..')) {
|
|
34
40
|
throw new Error(`SessionStore: invalid key ${JSON.stringify(key)}`);
|
|
35
41
|
}
|
|
36
|
-
return path.join(this.cacheDir, `${key}.json`);
|
|
42
|
+
return path.join(this.cacheDir, `${SessionStore.filenameEscape(key)}.json`);
|
|
43
|
+
}
|
|
44
|
+
/** 把 session key 转换成跨平台安全的 filename (escape Windows 非法字符). */
|
|
45
|
+
static filenameEscape(key) {
|
|
46
|
+
return key.replace(/:/g, '__');
|
|
47
|
+
}
|
|
48
|
+
/** listKeys 反向: 把 filename 还原成 session key. */
|
|
49
|
+
static filenameUnescape(filenameKey) {
|
|
50
|
+
return filenameKey.replace(/__/g, ':');
|
|
37
51
|
}
|
|
38
52
|
/**
|
|
39
53
|
* 写 history to disk.
|
|
@@ -116,7 +130,7 @@ export class SessionStore {
|
|
|
116
130
|
const files = await fs.readdir(this.cacheDir);
|
|
117
131
|
return files
|
|
118
132
|
.filter(f => f.endsWith('.json') && !f.endsWith('.tmp'))
|
|
119
|
-
.map(f => f.slice(0, -'.json'.length))
|
|
133
|
+
.map(f => SessionStore.filenameUnescape(f.slice(0, -'.json'.length)))
|
|
120
134
|
.sort();
|
|
121
135
|
}
|
|
122
136
|
catch {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* "commandAllowlist": ["git", "npm", "tsc", "vitest", "cat", "ls", "..."],
|
|
14
14
|
* "commandDenylist": ["rm", "mv", "chmod", "sudo", "su", "curl", "wget"],
|
|
15
15
|
* "pathAllowlist": [
|
|
16
|
-
* "src/web/client.
|
|
16
|
+
* "src/web/client.ts",
|
|
17
17
|
* "src/agents/workflow-engine.ts",
|
|
18
18
|
* "*.md",
|
|
19
19
|
* "docs/**"
|
|
@@ -58,7 +58,7 @@ const FALLBACK_COMMAND_ALLOWLIST = new Set([
|
|
|
58
58
|
]);
|
|
59
59
|
const FALLBACK_PATH_ALLOWLIST = [
|
|
60
60
|
// 自由区: AI 可以改
|
|
61
|
-
'src/web/client.
|
|
61
|
+
'src/web/client.ts',
|
|
62
62
|
'src/web/style.css',
|
|
63
63
|
'src/agents/workflow-engine.ts',
|
|
64
64
|
'src/agents/workflow-pivot-loop.ts',
|
|
@@ -120,10 +120,10 @@ export class WorkflowPivotLoop {
|
|
|
120
120
|
/**
|
|
121
121
|
* Execute the pivot loop
|
|
122
122
|
*/
|
|
123
|
-
async execute(input, llm, systemPrompt, streamCallback) {
|
|
123
|
+
async execute(input, llm, systemPrompt, streamCallback, signal) {
|
|
124
124
|
this.streamCallback = streamCallback;
|
|
125
125
|
this.state = this.createInitialState();
|
|
126
|
-
console.log(`[pivot] execute: input chars=${input.length}, systemPrompt chars=${systemPrompt.length}`);
|
|
126
|
+
console.log(`[pivot] execute: input chars=${input.length}, systemPrompt chars=${systemPrompt.length}, signal=${!!signal}`);
|
|
127
127
|
this.messageHistory = [{ role: 'user', content: input }];
|
|
128
128
|
// Analyze task complexity and adapt config
|
|
129
129
|
const taskProfile = analyzeTaskComplexity(input);
|
|
@@ -143,6 +143,15 @@ export class WorkflowPivotLoop {
|
|
|
143
143
|
});
|
|
144
144
|
let response = '';
|
|
145
145
|
while (this.shouldContinue(effectiveConfig)) {
|
|
146
|
+
// 2026-07-04: signal abort 让 pivot 提前退出 (防止 LLM hang)
|
|
147
|
+
if (signal?.aborted) {
|
|
148
|
+
this.emit({
|
|
149
|
+
type: 'status',
|
|
150
|
+
content: `⏹️ pivot loop 被 abort (iter=${this.state.iteration})`,
|
|
151
|
+
tool: 'loop'
|
|
152
|
+
});
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
146
155
|
this.state.iteration++;
|
|
147
156
|
this.emit({
|
|
148
157
|
type: 'status',
|
|
@@ -156,7 +165,7 @@ export class WorkflowPivotLoop {
|
|
|
156
165
|
try {
|
|
157
166
|
// Call LLM
|
|
158
167
|
const t0 = Date.now();
|
|
159
|
-
const llmResponse = await llm.chat(context, systemPrompt);
|
|
168
|
+
const llmResponse = await llm.chat(context, systemPrompt, signal);
|
|
160
169
|
const reply = llmResponse.reply.trim();
|
|
161
170
|
console.log(`[pivot] iter=${this.state.iteration} LLM took=${Date.now() - t0}ms reply=${reply.length} head=${reply.substring(0, 80).replace(/\n/g, ' ')}`);
|
|
162
171
|
this.emit({ type: 'token', content: reply.substring(0, 100) });
|