@steerable/agent-shell 0.6.29 → 0.6.30

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.
@@ -8,7 +8,7 @@ import { llmService, getSidecarSupervisor, whenSidecarSupervisor } from '../llm/
8
8
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
9
  import { buildWorldState, getActiveCoreLoopStreamId, streamCoreLoopTurn, } from './coreloop-stream.js';
10
10
  import { driveWithAutoContinue, resolveAutoContinueMax, } from './auto-continue-helper.js';
11
- import { buildDelegateDispatchInstruction, buildMentionDelegateRoster, mergeTurnSubagentParam, } from './subagent-profiles.js';
11
+ import { buildAmbientDelegateRoster, buildDelegateDispatchInstruction, buildDelegateRosterHint, buildMentionDelegateRoster, BUILTIN_SUBAGENT_PROFILES, mergeTurnSubagentParam, } from './subagent-profiles.js';
12
12
  import { resolveMentionedAgentIds } from './mention-targets.js';
13
13
  import { readSidecarHistoryEntries, timelineFromHistoryEntries, } from './task-process.js';
14
14
  import { appendTimelineDelta, freezeTimelineReasoning, sealLastTimelineBlock, syncTimelineTools, } from './turn-timeline.js';
@@ -1989,7 +1989,8 @@ export class LocalBackendRouter {
1989
1989
  let currentUserMessageId;
1990
1990
  // 本轮被点名的智能体:菜单点选带来的 id 优先,手打的 `@名字` 从正文
1991
1991
  // 解析补齐(两者缺一,提及就在后端消失,只剩前端徽章)。
1992
- const mentionedAgentIds = resolveMentionedAgentIds(payload, userMessageText, await this.store.listChatAgents());
1992
+ const chatAgents = await this.store.listChatAgents();
1993
+ const mentionedAgentIds = resolveMentionedAgentIds(payload, userMessageText, chatAgents);
1993
1994
  if (!regenerateMatch && !isResume) {
1994
1995
  const userMeta = mentionedAgentIds.length > 0
1995
1996
  ? JSON.stringify({ mentionedAgentIds })
@@ -2047,8 +2048,22 @@ export class LocalBackendRouter {
2047
2048
  available: turnTools.some((t) => t.name === token),
2048
2049
  };
2049
2050
  }
2050
- const mentionRoster = await buildMentionDelegateRoster(turnAgents.delegates, turnTools.map((tool) => tool.name));
2051
- const { systemPrompt, messages, skillContext } = await this.buildConversationMessages(chatId, cleanUserMessageText, payload, turnTools, turnAgents, forcedSkillName, chatMode, forcedMcpTool, currentUserMessageId, mentionRoster);
2051
+ const turnToolNames = turnTools.map((tool) => tool.name);
2052
+ const mentionRoster = await buildMentionDelegateRoster(turnAgents.delegates, turnToolNames);
2053
+ // 常驻可委派名单:其余智能体也进画像,技能/提示里的「交给 X」无需用户
2054
+ // `@` 就能落成一次真实委派。父代理自己不进——它就是本轮的执行者,给它
2055
+ // 一个自己的画像只会诱导无意义的自委派(`@自己` 走提及那条路,仍然可以)。
2056
+ const ambientRoster = await buildAmbientDelegateRoster(chatAgents, turnToolNames, {
2057
+ excludeAgentIds: [
2058
+ ...mentionRoster.map((row) => row.agentId),
2059
+ ...(turnAgents.parent ? [turnAgents.parent.id] : []),
2060
+ ],
2061
+ reservedProfileNames: [
2062
+ ...Object.keys(BUILTIN_SUBAGENT_PROFILES),
2063
+ ...mentionRoster.map((row) => row.profileName),
2064
+ ],
2065
+ });
2066
+ const { systemPrompt, messages, skillContext } = await this.buildConversationMessages(chatId, cleanUserMessageText, payload, turnTools, turnAgents, forcedSkillName, chatMode, forcedMcpTool, currentUserMessageId, { mention: mentionRoster, ambient: ambientRoster });
2052
2067
  // A4: the sidecar-hosted CoreLoop is the only chat path (the TS loop
2053
2068
  // was deleted 2026-08-26 after default-on + canary verification). Tools
2054
2069
  // round-trip back to this process over the reverse channel. If the
@@ -2071,7 +2086,7 @@ export class LocalBackendRouter {
2071
2086
  shouldGenerateTitle,
2072
2087
  firstUserMessageForTitle,
2073
2088
  resume: isResume,
2074
- subagent: mergeTurnSubagentParam(Object.fromEntries(mentionRoster.map((row) => [row.profileName, row.profile]))),
2089
+ subagent: mergeTurnSubagentParam(Object.fromEntries(mentionRoster.map((row) => [row.profileName, row.profile])), Object.fromEntries(ambientRoster.map((row) => [row.profileName, row.profile]))),
2075
2090
  parentAgentId: turnAgents.parent?.id ?? null,
2076
2091
  });
2077
2092
  }
@@ -2206,7 +2221,7 @@ export class LocalBackendRouter {
2206
2221
  capability: mergeAgentCapabilities(parent ? [parent] : []),
2207
2222
  };
2208
2223
  }
2209
- async buildConversationMessages(chatId, latestUserMessage, payload, turnTools, turnAgents, forcedSkillName, chatMode = 'agent', forcedMcpTool, currentUserMessageId, mentionRoster = []) {
2224
+ async buildConversationMessages(chatId, latestUserMessage, payload, turnTools, turnAgents, forcedSkillName, chatMode = 'agent', forcedMcpTool, currentUserMessageId, delegates = { mention: [], ambient: [] }) {
2210
2225
  // 跨轮压缩由框架 CoreLoop 拥有:token 压力触发 CompactionHooks(已接真实
2211
2226
  // summarizer),压缩边界持久化到 durable record,W6-10 透视 reconcile 保证
2212
2227
  // 压缩跨轮存活。桌面把全量原始历史作为种子发给框架——不再维护桌面侧滚动
@@ -2276,7 +2291,11 @@ export class LocalBackendRouter {
2276
2291
  .join('\n\n');
2277
2292
  const polluted = this.detectToolDenialInHistory(historyAssistantTexts);
2278
2293
  const runtimeEnvironment = this.buildRuntimeEnvironmentContext(chatMode);
2279
- const realityCheck = runtimeEnvironment + this.buildToolRealityCheck(turnTools, polluted, chatMode);
2294
+ // 名录进 realityCheck:两条系统提示拼装路径(技能拼装 / 用户整段覆盖)
2295
+ // 都会带上它,且位置在末尾——不动技能正文那段 prompt cache 前缀。
2296
+ const realityCheck = runtimeEnvironment +
2297
+ this.buildToolRealityCheck(turnTools, polluted, chatMode) +
2298
+ buildDelegateRosterHint([...delegates.mention, ...delegates.ambient]);
2280
2299
  // 用户显式覆盖(payload.systemPrompt 优先 / settings.systemPrompt 自定义了且非默认值次之)走
2281
2300
  // "整段替换"路径,保持旧行为可被外部完全控制;否则交给 skill-based
2282
2301
  // SystemPromptBuilder 根据本轮可用工具动态拼装。
@@ -2407,8 +2426,8 @@ export class LocalBackendRouter {
2407
2426
  if (finalUserImages.notes.length > 0) {
2408
2427
  finalUserContent = `${finalUserContent}\n\n【附件图片】\n${finalUserImages.notes.join('\n')}`;
2409
2428
  }
2410
- if (mentionRoster.length > 0) {
2411
- finalUserContent = `${finalUserContent}\n\n${buildDelegateDispatchInstruction(mentionRoster.map((row) => ({
2429
+ if (delegates.mention.length > 0) {
2430
+ finalUserContent = `${finalUserContent}\n\n${buildDelegateDispatchInstruction(delegates.mention.map((row) => ({
2412
2431
  name: row.name,
2413
2432
  profileName: row.profileName,
2414
2433
  toolFilter: row.profile.toolFilter,
@@ -29,8 +29,14 @@ export interface SubagentAgentInput extends AgentCapabilityInput {
29
29
  name: string;
30
30
  rolePrompt: string | null;
31
31
  description?: string | null;
32
+ /** 画像缓存的失效依据:改配置即换键。缺省则该智能体的画像每轮重建。 */
33
+ updatedAt?: string;
32
34
  }
33
- /** 一次 `@` 提及转出的画像,连同显示名,供派发指令与 sidecar 参数共用。 */
35
+ /**
36
+ * 一个可委派对象的画像,连同显示名,供派发指令、名录说明与 sidecar 参数
37
+ * 共用。两个来源共享这个结构:用户 `@` 点名的(强制派发)与常驻可委派
38
+ * 名单里的(模型自主判断)。
39
+ */
34
40
  export interface MentionDelegateProfile {
35
41
  /** 被提及智能体的 id——调用方据此认出自提及那一行。 */
36
42
  agentId: string;
@@ -83,6 +89,28 @@ export declare function buildMentionSubagentProfiles(delegates: readonly Subagen
83
89
  * @returns 提及顺序的画像花名册。
84
90
  */
85
91
  export declare function buildMentionDelegateRoster(delegates: readonly SubagentAgentInput[], parentToolNames: readonly string[]): Promise<MentionDelegateProfile[]>;
92
+ /**
93
+ * 常驻可委派名单:会话里的其他智能体也转成画像,模型**无需用户 `@`** 就能
94
+ * 委派。技能正文或自定义提示里写「交给 X」因此能落成一次真实
95
+ * `delegate_subagent`——在此之前 `subagent_type` 的 enum 里只有内置三个画像,
96
+ * 这类指令 fail closed 报 `unknown subagent_type`,模型只能在正文里打出名字。
97
+ *
98
+ * 与提及名单的唯一区别是强制性:这里**不进** `requiredProfiles`。派不派由模型
99
+ * 按任务判断——技能的阶段条件(「阶段 C 才交给 Word 智能体」)在拼装画像时
100
+ * 还不可知,一律强制会把不该派的回合反复退回完成门。
101
+ *
102
+ * @param agents 会话可用的智能体(未归档)。
103
+ * @param parentToolNames 父本轮模型可见工具名。
104
+ * @param options.excludeAgentIds 不进名单的智能体:父代理自己与已在提及名单里的。
105
+ * @param options.reservedProfileNames 已占用的画像名(内置画像 + 提及画像),避免顶掉。
106
+ * @returns 画像花名册,按 `agents` 顺序。
107
+ */
108
+ export declare function buildAmbientDelegateRoster(agents: readonly SubagentAgentInput[], parentToolNames: readonly string[], options?: {
109
+ excludeAgentIds?: readonly string[];
110
+ reservedProfileNames?: readonly string[];
111
+ }): Promise<MentionDelegateProfile[]>;
112
+ /** 清空画像缓存(测试用;生产靠 `updatedAt` 与存活时长失效)。 */
113
+ export declare function resetDelegateProfileCache(): void;
86
114
  /**
87
115
  * 注入本轮最后一条用户消息的强制派发指令。
88
116
  *
@@ -99,6 +127,24 @@ export declare function buildDelegateDispatchInstruction(delegates: ReadonlyArra
99
127
  toolFilter?: string[];
100
128
  isSelf?: boolean;
101
129
  }>): string;
130
+ /**
131
+ * 可委派智能体名录:注入系统提示(realityCheck 段)。
132
+ *
133
+ * 技能正文与自定义提示里写的是**显示名**(`@Word智能体`),而 `subagent_type`
134
+ * 只认 ASCII 画像名(`word-master`)。没有这张对照表,技能作者只能硬编码画像
135
+ * 名、智能体一改名就断;模型也无从知道正文里的「@某智能体」该落成一次
136
+ * `delegate_subagent`,于是只在回复里打出这个名字然后收尾。
137
+ *
138
+ * 与 {@link buildDelegateDispatchInstruction} 的分工:那份是用户点名后的**强制**
139
+ * 派发指令(配 `requiredProfiles` 完成门兜底),这份只说明「能派谁、怎么派」。
140
+ *
141
+ * @param delegates 本轮可委派的智能体(提及 + 常驻名单)。
142
+ * @returns 名录正文;空名单返回空串,本轮系统提示与改前逐字节一致。
143
+ */
144
+ export declare function buildDelegateRosterHint(delegates: ReadonlyArray<{
145
+ name: string;
146
+ profileName: string;
147
+ }>): string;
102
148
  export interface TurnSubagentParam {
103
149
  profiles: Record<string, BuiltinSubagentProfile>;
104
150
  maxParallel?: number;
@@ -106,12 +152,14 @@ export interface TurnSubagentParam {
106
152
  requiredProfiles?: string[];
107
153
  }
108
154
  /**
109
- * 内置画像 + 本轮提及画像。被 `@` 超过 4 个时抬高池的并行上限。
155
+ * 内置画像 + 常驻可委派画像 + 本轮提及画像。被 `@` 超过 4 个时抬高池的并行上限。
110
156
  *
111
- * 提及画像同时作为 `requiredProfiles` 下发:强制派发此前只是提示词里的
112
- * 一句话,模型跑了别的工具再叙述「已启动」就能蒙过所有既有纪律检查。
157
+ * 只有**提及**画像进 `requiredProfiles`:强制派发此前只是提示词里的一句话,
158
+ * 模型跑了别的工具再叙述「已启动」就能蒙过所有既有纪律检查。常驻画像刻意不进
159
+ * ——它们是「可以派」,不是「本轮必须派」。
113
160
  *
114
- * @param mentionProfiles 本轮提及转出的画像;空对象则与 {@link builtinSubagentParam} 相同。
161
+ * @param mentionProfiles 本轮提及转出的画像。
162
+ * @param ambientProfiles 常驻可委派画像;与提及画像同名时以提及为准。
115
163
  * @returns 下发给 sidecar 的 `subagent` 参数。
116
164
  */
117
- export declare function mergeTurnSubagentParam(mentionProfiles: Record<string, BuiltinSubagentProfile>): TurnSubagentParam;
165
+ export declare function mergeTurnSubagentParam(mentionProfiles: Record<string, BuiltinSubagentProfile>, ambientProfiles?: Record<string, BuiltinSubagentProfile>): TurnSubagentParam;
@@ -148,9 +148,35 @@ export async function buildMentionSubagentProfiles(delegates, parentToolNames) {
148
148
  * @returns 提及顺序的画像花名册。
149
149
  */
150
150
  export async function buildMentionDelegateRoster(delegates, parentToolNames) {
151
+ return await buildRoster(delegates, parentToolNames, new Set());
152
+ }
153
+ /**
154
+ * 常驻可委派名单:会话里的其他智能体也转成画像,模型**无需用户 `@`** 就能
155
+ * 委派。技能正文或自定义提示里写「交给 X」因此能落成一次真实
156
+ * `delegate_subagent`——在此之前 `subagent_type` 的 enum 里只有内置三个画像,
157
+ * 这类指令 fail closed 报 `unknown subagent_type`,模型只能在正文里打出名字。
158
+ *
159
+ * 与提及名单的唯一区别是强制性:这里**不进** `requiredProfiles`。派不派由模型
160
+ * 按任务判断——技能的阶段条件(「阶段 C 才交给 Word 智能体」)在拼装画像时
161
+ * 还不可知,一律强制会把不该派的回合反复退回完成门。
162
+ *
163
+ * @param agents 会话可用的智能体(未归档)。
164
+ * @param parentToolNames 父本轮模型可见工具名。
165
+ * @param options.excludeAgentIds 不进名单的智能体:父代理自己与已在提及名单里的。
166
+ * @param options.reservedProfileNames 已占用的画像名(内置画像 + 提及画像),避免顶掉。
167
+ * @returns 画像花名册,按 `agents` 顺序。
168
+ */
169
+ export async function buildAmbientDelegateRoster(agents, parentToolNames, options = {}) {
170
+ const excluded = new Set(options.excludeAgentIds ?? []);
171
+ const candidates = agents.filter((agent) => !excluded.has(agent.id));
172
+ if (candidates.length === 0)
173
+ return [];
174
+ return await buildRoster(candidates, parentToolNames, new Set(options.reservedProfileNames ?? []));
175
+ }
176
+ async function buildRoster(agents, parentToolNames, reserved) {
151
177
  const roster = [];
152
- const used = new Set();
153
- for (const agent of delegates) {
178
+ const used = new Set(reserved);
179
+ for (const agent of agents) {
154
180
  let profileName = profileNameForAgent(agent);
155
181
  if (used.has(profileName)) {
156
182
  const suffix = agent.id.replace(/[^a-zA-Z0-9]/g, '').slice(0, 8);
@@ -161,12 +187,52 @@ export async function buildMentionDelegateRoster(delegates, parentToolNames) {
161
187
  agentId: agent.id,
162
188
  name: agent.name,
163
189
  profileName,
164
- profile: await buildOneMentionProfile(agent, parentToolNames),
190
+ profile: await buildOneDelegateProfile(agent, parentToolNames),
165
191
  });
166
192
  }
167
193
  return roster;
168
194
  }
169
- async function buildOneMentionProfile(agent, parentToolNames) {
195
+ /**
196
+ * 画像构建缓存的存活时长。
197
+ *
198
+ * 常驻名单让每轮的画像数量从「用户点了几个」变成「用户有几个智能体」,而
199
+ * 每份画像的 `systemPrompt` 都要过一次 `buildSystemPrompt`(内含技能目录
200
+ * 扫描)——不缓存就是每轮多做 N 次磁盘扫描,直接加在首个 token 之前。
201
+ *
202
+ * 缓存键带 `updatedAt`,所以改智能体立即生效;技能**正文**落盘不改
203
+ * `updatedAt`,靠这个存活时长兜底,最迟下一轮生效。
204
+ */
205
+ const DELEGATE_PROFILE_TTL_MS = 15_000;
206
+ const delegateProfileCache = new Map();
207
+ /** 清空画像缓存(测试用;生产靠 `updatedAt` 与存活时长失效)。 */
208
+ export function resetDelegateProfileCache() {
209
+ delegateProfileCache.clear();
210
+ }
211
+ async function buildOneDelegateProfile(agent, parentToolNames) {
212
+ // 键要完整描述这份画像的输入:身份(改名/换 slug 都会换画像名与人设)、
213
+ // `updatedAt`(改配置即失效)、父工具面(`toolFilter` 与画像 systemPrompt
214
+ // 里的工具名录都由它派生)。只用 id 的话,任何复用 id 的调用方(内存假
215
+ // 存储的测试)都会静默拿到另一个智能体的画像。
216
+ const cacheKey = agent.updatedAt
217
+ ? [
218
+ agent.id,
219
+ agent.updatedAt,
220
+ agent.slug ?? '',
221
+ agent.name,
222
+ parentToolNames.join(','),
223
+ ].join('\u0000')
224
+ : null;
225
+ if (cacheKey) {
226
+ const hit = delegateProfileCache.get(cacheKey);
227
+ if (hit && Date.now() - hit.builtAtMs < DELEGATE_PROFILE_TTL_MS)
228
+ return hit.profile;
229
+ }
230
+ const profile = await buildProfileUncached(agent, parentToolNames);
231
+ if (cacheKey)
232
+ delegateProfileCache.set(cacheKey, { builtAtMs: Date.now(), profile });
233
+ return profile;
234
+ }
235
+ async function buildProfileUncached(agent, parentToolNames) {
170
236
  const toolFilter = resolveChildToolFilter(agent.toolPolicy, parentToolNames);
171
237
  const domain = toolFilter ?? parentToolNames;
172
238
  const readOnly = isReadOnlyToolDomain(domain);
@@ -232,18 +298,54 @@ export function buildDelegateDispatchInstruction(delegates) {
232
298
  ].join('\n');
233
299
  }
234
300
  /**
235
- * 内置画像 + 本轮提及画像。被 `@` 超过 4 个时抬高池的并行上限。
301
+ * 可委派智能体名录:注入系统提示(realityCheck 段)。
302
+ *
303
+ * 技能正文与自定义提示里写的是**显示名**(`@Word智能体`),而 `subagent_type`
304
+ * 只认 ASCII 画像名(`word-master`)。没有这张对照表,技能作者只能硬编码画像
305
+ * 名、智能体一改名就断;模型也无从知道正文里的「@某智能体」该落成一次
306
+ * `delegate_subagent`,于是只在回复里打出这个名字然后收尾。
307
+ *
308
+ * 与 {@link buildDelegateDispatchInstruction} 的分工:那份是用户点名后的**强制**
309
+ * 派发指令(配 `requiredProfiles` 完成门兜底),这份只说明「能派谁、怎么派」。
310
+ *
311
+ * @param delegates 本轮可委派的智能体(提及 + 常驻名单)。
312
+ * @returns 名录正文;空名单返回空串,本轮系统提示与改前逐字节一致。
313
+ */
314
+ export function buildDelegateRosterHint(delegates) {
315
+ if (delegates.length === 0)
316
+ return '';
317
+ const roster = delegates
318
+ .map((row) => `- ${row.name} → \`subagent_type="${row.profileName}"\``)
319
+ .join('\n');
320
+ return [
321
+ '',
322
+ '',
323
+ '## 可委派的智能体',
324
+ '',
325
+ '以下智能体可以通过 `delegate_subagent` 接活,画像名对照:',
326
+ '',
327
+ roster,
328
+ '',
329
+ '- 指令(含技能正文)里出现「@某智能体」或「交给某智能体」时,意思是**用 `delegate_subagent` 把这份活派给它**,`subagent_type` 填上表对应的画像名——不是在回复里打出这个名字。',
330
+ '- 每份 `task` 必须自包含(目标、输入、交付形式、验收标准):子代理看不到本对话。',
331
+ '- 表里没有的名字不要猜着填,`subagent_type` 只接受上表与内置画像。',
332
+ ].join('\n');
333
+ }
334
+ /**
335
+ * 内置画像 + 常驻可委派画像 + 本轮提及画像。被 `@` 超过 4 个时抬高池的并行上限。
236
336
  *
237
- * 提及画像同时作为 `requiredProfiles` 下发:强制派发此前只是提示词里的
238
- * 一句话,模型跑了别的工具再叙述「已启动」就能蒙过所有既有纪律检查。
337
+ * 只有**提及**画像进 `requiredProfiles`:强制派发此前只是提示词里的一句话,
338
+ * 模型跑了别的工具再叙述「已启动」就能蒙过所有既有纪律检查。常驻画像刻意不进
339
+ * ——它们是「可以派」,不是「本轮必须派」。
239
340
  *
240
- * @param mentionProfiles 本轮提及转出的画像;空对象则与 {@link builtinSubagentParam} 相同。
341
+ * @param mentionProfiles 本轮提及转出的画像。
342
+ * @param ambientProfiles 常驻可委派画像;与提及画像同名时以提及为准。
241
343
  * @returns 下发给 sidecar 的 `subagent` 参数。
242
344
  */
243
- export function mergeTurnSubagentParam(mentionProfiles) {
345
+ export function mergeTurnSubagentParam(mentionProfiles, ambientProfiles = {}) {
244
346
  const mentionNames = Object.keys(mentionProfiles);
245
347
  return {
246
- profiles: { ...BUILTIN_SUBAGENT_PROFILES, ...mentionProfiles },
348
+ profiles: { ...BUILTIN_SUBAGENT_PROFILES, ...ambientProfiles, ...mentionProfiles },
247
349
  ...(mentionNames.length > 4 ? { maxParallel: mentionNames.length } : {}),
248
350
  ...(mentionNames.length > 0 ? { requiredProfiles: mentionNames } : {}),
249
351
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerable/agent-shell",
3
- "version": "0.6.29",
3
+ "version": "0.6.30",
4
4
  "description": "Steerable framework — product-neutral desktop/headless agent host (Tier 5). Electron main + preload + headless HTTP server (BS) sharing one storage/sidecar/tooling core; scenario packs extend it through the @steerable/pack-sdk contract and are composed at build time by the consuming product.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://steerableframework.com/",
@@ -49,9 +49,9 @@
49
49
  "electron-store": "^8.1.0",
50
50
  "he": "^1.2.0",
51
51
  "node-pty": "^1.1.0",
52
- "@steerable/agent-harness": "0.6.29",
53
- "@steerable/pack-sdk": "0.6.29",
54
- "@steerable/agent-protocol": "0.6.29"
52
+ "@steerable/agent-harness": "0.6.30",
53
+ "@steerable/pack-sdk": "0.6.30",
54
+ "@steerable/agent-protocol": "0.6.30"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@types/better-sqlite3": "^7.6.13",