@octopi-agent/engine 0.90.11 → 0.97.15
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/config-admin/agent-presets.d.ts +70 -0
- package/dist/config-admin/agent-presets.js +243 -0
- package/dist/config-admin/discover.d.ts +30 -0
- package/dist/config-admin/discover.js +48 -0
- package/dist/config-admin/env.d.ts +22 -0
- package/dist/config-admin/env.js +41 -0
- package/dist/config-admin/index.d.ts +20 -0
- package/dist/config-admin/index.js +15 -0
- package/dist/config-admin/model-presets.d.ts +75 -0
- package/dist/config-admin/model-presets.js +249 -0
- package/dist/config-admin/name-guard.d.ts +14 -0
- package/dist/config-admin/name-guard.js +24 -0
- package/dist/config-admin/patch.d.ts +56 -0
- package/dist/config-admin/patch.js +201 -0
- package/dist/config-admin/port.d.ts +68 -0
- package/dist/config-admin/port.js +21 -0
- package/dist/config-admin/redact.d.ts +39 -0
- package/dist/config-admin/redact.js +42 -0
- package/dist/config-schema/agent.d.ts +43 -1
- package/dist/config-schema/context.d.ts +6 -1
- package/dist/config-schema/context.js +8 -0
- package/dist/config-schema/engine.d.ts +106 -4
- package/dist/config-schema/governance.d.ts +43 -1
- package/dist/config-schema/governance.js +157 -0
- package/dist/config-schema/models.d.ts +5 -2
- package/dist/config-schema/models.js +4 -1
- package/dist/config-schema/observability.d.ts +9 -0
- package/dist/config-schema/observability.js +29 -0
- package/dist/config-schema/run.d.ts +3 -0
- package/dist/config-schema/run.js +14 -0
- package/dist/config.d.ts +29 -4
- package/dist/config.js +15 -4
- package/dist/harness/activation/dispatcher.d.ts +8 -2
- package/dist/harness/activation/dispatcher.js +33 -5
- package/dist/harness/activation/inbox.d.ts +87 -0
- package/dist/harness/activation/inbox.js +169 -0
- package/dist/harness/activation/runtime.d.ts +9 -8
- package/dist/harness/activation/runtime.js +100 -8
- package/dist/harness/activation/types.d.ts +19 -0
- package/dist/harness/activation/types.js +2 -0
- package/dist/harness/agent/builder.d.ts +15 -1
- package/dist/harness/agent/builder.js +31 -10
- package/dist/harness/agent/config-bridge.d.ts +5 -0
- package/dist/harness/agent/config-bridge.js +3 -0
- package/dist/harness/agent/config-integrity.d.ts +72 -0
- package/dist/harness/agent/config-integrity.js +146 -0
- package/dist/harness/agent/wire-agent-builder.js +2 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +3 -1
- package/dist/harness/collaboration/delegation/child-run.js +21 -5
- package/dist/harness/collaboration/delegation/notifier.js +4 -3
- package/dist/harness/collaboration/delegation/service.js +2 -3
- package/dist/harness/collaboration/delegation/wiring.js +6 -5
- package/dist/harness/collaboration/orchestration/reflector/llm-reflector.d.ts +1 -1
- package/dist/harness/collaboration/orchestration/reflector/llm-reflector.js +1 -1
- package/dist/harness/context/capabilities/compact/engine.d.ts +30 -0
- package/dist/harness/context/capabilities/compact/engine.js +198 -46
- package/dist/harness/context/capabilities/compact/handover.d.ts +78 -0
- package/dist/harness/context/capabilities/compact/handover.js +107 -0
- package/dist/harness/context/capabilities/compact/index.d.ts +3 -1
- package/dist/harness/context/capabilities/compact/index.js +2 -1
- package/dist/harness/context/capabilities/compact/types.d.ts +30 -1
- package/dist/harness/context/capabilities/summary/executor.js +2 -1
- package/dist/harness/context/capabilities/summary/policies.js +8 -5
- package/dist/harness/context/constitution/default-agents.md +11 -10
- package/dist/harness/context/default-context-engine.d.ts +34 -4
- package/dist/harness/context/default-context-engine.js +194 -49
- package/dist/harness/context/hybrid-compressor.js +6 -0
- package/dist/harness/context/llm-summarizer.d.ts +2 -8
- package/dist/harness/context/llm-summarizer.js +69 -91
- package/dist/harness/context/message-selector.js +4 -3
- package/dist/harness/context/summarize.d.ts +2 -0
- package/dist/harness/context/summarize.js +7 -2
- package/dist/harness/context/truncate-compressor.js +7 -0
- package/dist/harness/context/turn-envelope.d.ts +3 -2
- package/dist/harness/context/turn-envelope.js +3 -2
- package/dist/harness/context/types.d.ts +33 -1
- package/dist/harness/extension/execution-environment/dangerous-commands.d.ts +28 -0
- package/dist/harness/extension/execution-environment/dangerous-commands.js +129 -0
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +127 -8
- package/dist/harness/extension/execution-environment/effect-policy.js +244 -19
- package/dist/harness/extension/execution-environment/index.d.ts +4 -2
- package/dist/harness/extension/execution-environment/index.js +2 -1
- package/dist/harness/extension/execution-environment/kill-switch.d.ts +34 -0
- package/dist/harness/extension/execution-environment/kill-switch.js +46 -0
- package/dist/harness/extension/execution-environment/ops-audit.d.ts +200 -0
- package/dist/harness/extension/execution-environment/ops-audit.js +338 -0
- package/dist/harness/extension/execution-environment/privileged-exec.d.ts +158 -0
- package/dist/harness/extension/execution-environment/privileged-exec.js +418 -0
- package/dist/harness/extension/execution-environment/process-info.d.ts +180 -0
- package/dist/harness/extension/execution-environment/process-info.js +466 -0
- package/dist/harness/extension/execution-environment/process-isolation.d.ts +7 -0
- package/dist/harness/extension/execution-environment/process-isolation.js +9 -0
- package/dist/harness/extension/execution-environment/sandbox.d.ts +109 -0
- package/dist/harness/extension/execution-environment/sandbox.js +228 -21
- package/dist/harness/extension/execution-environment/tool-capabilities.d.ts +1 -1
- package/dist/harness/extension/execution-environment/tool-capabilities.js +6 -0
- package/dist/harness/extension/execution-environment/types.d.ts +19 -0
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.js +33 -0
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.d.ts +4 -5
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.js +5 -15
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +3 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +53 -2
- package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/index.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/privileged-tool.d.ts +43 -0
- package/dist/harness/extension/plugin-ecosystem/tools/privileged-tool.js +350 -0
- package/dist/harness/extension/plugin-ecosystem/tools/process-tools.d.ts +30 -0
- package/dist/harness/extension/plugin-ecosystem/tools/process-tools.js +137 -0
- package/dist/harness/governance/human-in-the-loop/decision-cache.d.ts +54 -4
- package/dist/harness/governance/human-in-the-loop/decision-cache.js +42 -16
- package/dist/harness/governance/security/temp-path.d.ts +9 -0
- package/dist/harness/governance/security/temp-path.js +13 -2
- package/dist/harness/governance/session-acl/seed-roles.js +14 -0
- package/dist/harness/index.d.ts +2 -0
- package/dist/harness/index.js +2 -0
- package/dist/harness/knowledge/fts.js +1 -2
- package/dist/harness/knowledge/index-store.js +59 -9
- package/dist/harness/memory/sqlite/keyword-search.d.ts +1 -0
- package/dist/harness/memory/sqlite/keyword-search.js +4 -3
- package/dist/harness/observability/observer/hub.js +8 -0
- package/dist/harness/observability/observer/run-snapshot.js +1 -0
- package/dist/harness/observability/observer/types.d.ts +4 -0
- package/dist/harness/run/agent/agent.d.ts +7 -0
- package/dist/harness/run/agent/agent.js +23 -1
- package/dist/harness/run/reliability/fallback-provider.d.ts +117 -5
- package/dist/harness/run/reliability/fallback-provider.js +262 -22
- package/dist/harness/run/reliability/harness-events.d.ts +24 -1
- package/dist/harness/run/reliability/model-error-policy.d.ts +32 -0
- package/dist/harness/run/reliability/model-error-policy.js +42 -0
- package/dist/harness/run/reliability/provider-wrapper.js +29 -2
- package/dist/harness/run/reliability/refusal-guard.d.ts +20 -0
- package/dist/harness/run/reliability/refusal-guard.js +102 -0
- package/dist/harness/run/reliability/run-agent.js +231 -33
- package/dist/harness/run/run-guard/default-run-guard.js +1 -1
- package/dist/harness/run/run-scope.d.ts +31 -0
- package/dist/harness/run/runner.d.ts +116 -2
- package/dist/harness/run/runner.js +547 -41
- package/dist/harness/session/attachments/llm-intent.js +2 -1
- package/dist/harness/session/tasks/render.d.ts +4 -1
- package/dist/harness/session/tasks/render.js +10 -7
- package/dist/harness/shared/events/agent-event-map.d.ts +66 -0
- package/dist/harness/shared/events/agent-event-map.js +5 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/integration/agent-runtime/channel-message-source.js +19 -1
- package/dist/integration/index.d.ts +2 -0
- package/dist/integration/index.js +1 -0
- package/dist/integration/observability/loop-watchdog.d.ts +56 -0
- package/dist/integration/observability/loop-watchdog.js +177 -0
- package/dist/integration/observability/metrics.d.ts +3 -0
- package/dist/integration/observability/metrics.js +18 -7
- package/dist/integration/providers/anthropic.d.ts +23 -0
- package/dist/integration/providers/anthropic.js +97 -12
- package/dist/integration/providers/fetch-timeout.d.ts +42 -0
- package/dist/integration/providers/fetch-timeout.js +51 -0
- package/dist/integration/providers/openai.d.ts +10 -0
- package/dist/integration/providers/openai.js +58 -11
- package/dist/integration/providers/provider-diagnostics.d.ts +80 -0
- package/dist/integration/providers/provider-diagnostics.js +89 -0
- package/dist/integration/providers/timeout-coherence.d.ts +52 -0
- package/dist/integration/providers/timeout-coherence.js +68 -0
- package/dist/integration/providers/wire-dump.d.ts +62 -0
- package/dist/integration/providers/wire-dump.js +111 -2
- package/dist/integration/storage/jsonl.js +4 -1
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +1 -1
- package/dist/subsystems/memory-steward/backfill/handler.js +1 -1
- package/dist/subsystems/wisdom-steward/formation/handler.js +1 -1
- package/package.json +3 -2
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* 设计:arch/agent-runtime.md
|
|
8
8
|
*/
|
|
9
9
|
import { CoalesceBuffer } from './coalesce.js';
|
|
10
|
-
import { buildRunRequest, resolveSessionId } from './compiler.js';
|
|
10
|
+
import { buildRunRequest, compileMessages, resolveSessionId } from './compiler.js';
|
|
11
11
|
import { ExplicitRouter } from './router.js';
|
|
12
12
|
import { RuntimeEvents } from './types.js';
|
|
13
13
|
/**
|
|
@@ -156,8 +156,9 @@ export class AgentRuntime {
|
|
|
156
156
|
return results;
|
|
157
157
|
}
|
|
158
158
|
/**
|
|
159
|
-
* 模型 A:await 至 Run 结束(或 skipped / failed)
|
|
159
|
+
* 模型 A:await 至 Run 结束(或 skipped / failed / accepted)
|
|
160
160
|
* @param options.onEvent - 转发 Run 事件流(Gateway 流式广播);不变量 #6
|
|
161
|
+
* @param options.onDelivery - 活跃 Run 接收即回执(早于 Run 结束)
|
|
161
162
|
*/
|
|
162
163
|
async dispatch(trigger, options) {
|
|
163
164
|
if (!this.started) {
|
|
@@ -183,8 +184,22 @@ export class AgentRuntime {
|
|
|
183
184
|
});
|
|
184
185
|
return { status: 'skipped', reason: 'no_agent' };
|
|
185
186
|
}
|
|
187
|
+
// 回执恒发一次:fan-out 多 target 时首个去向生效,其余静默
|
|
188
|
+
//(每 target 一帧 accepted 会与 channel/web 的「一次受理」契约冲突)
|
|
189
|
+
let deliveryFired = false;
|
|
190
|
+
const deliveryOptions = options
|
|
191
|
+
? {
|
|
192
|
+
...options,
|
|
193
|
+
onDelivery: (delivery) => {
|
|
194
|
+
if (deliveryFired)
|
|
195
|
+
return;
|
|
196
|
+
deliveryFired = true;
|
|
197
|
+
options.onDelivery?.(delivery);
|
|
198
|
+
},
|
|
199
|
+
}
|
|
200
|
+
: undefined;
|
|
186
201
|
if (targets.length === 1) {
|
|
187
|
-
const result = await this.dispatchToTarget(trigger, targets[0],
|
|
202
|
+
const result = await this.dispatchToTarget(trigger, targets[0], deliveryOptions);
|
|
188
203
|
// 单目标丢弃也必须留下 TRIGGER_DROPPED(与 fan-out 对称):
|
|
189
204
|
// 「trigger 未被受理必有 dropped 事件」是委派送达回执(delegation §7.1)
|
|
190
205
|
// 依赖的不变量,缺事件 = 送达回执无法证伪。受理后 run 失败走 RUN_FAILED
|
|
@@ -201,11 +216,11 @@ export class AgentRuntime {
|
|
|
201
216
|
// fan-out:串行;聚合为 FanoutDispatchResult(ran/aborted/failed 均不静默丢弃)
|
|
202
217
|
const results = [];
|
|
203
218
|
for (const target of targets) {
|
|
204
|
-
const result = await this.dispatchToTarget(trigger, target,
|
|
219
|
+
const result = await this.dispatchToTarget(trigger, target, deliveryOptions);
|
|
205
220
|
results.push({ agentId: target.agentId, result });
|
|
206
221
|
}
|
|
207
222
|
const anyFailed = results.some((r) => r.result.status === 'failed');
|
|
208
|
-
const anyRan = results.some((r) => r.result.status === 'ran');
|
|
223
|
+
const anyRan = results.some((r) => r.result.status === 'ran' || r.result.status === 'accepted');
|
|
209
224
|
const allSkipped = results.every((r) => r.result.status === 'skipped');
|
|
210
225
|
if (anyFailed) {
|
|
211
226
|
this.emitRuntime(RuntimeEvents.TRIGGER_DROPPED, {
|
|
@@ -234,6 +249,40 @@ export class AgentRuntime {
|
|
|
234
249
|
}
|
|
235
250
|
return { status: 'skipped', reason: 'no_agent' };
|
|
236
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* 路由目标的活跃 inbox 接收(human 与 delegation signal 共用)。
|
|
254
|
+
* 仅 message / agent_signal 可接续;其余类型与带附件/换模型输入走完整新 Run。
|
|
255
|
+
*/
|
|
256
|
+
offerToTarget(trigger, target, sessionId, onEvent) {
|
|
257
|
+
if (!target.dispatcher.inbox)
|
|
258
|
+
return 'dispatched';
|
|
259
|
+
if (trigger.type !== 'message' && trigger.type !== 'agent_signal')
|
|
260
|
+
return 'dispatched';
|
|
261
|
+
const [compiledMessage] = compileMessages([trigger]);
|
|
262
|
+
const human = trigger.type === 'message' && trigger.payload.kind === 'user_message';
|
|
263
|
+
const delegationId = trigger.metadata?.delegationId;
|
|
264
|
+
return target.dispatcher.inbox.offer({
|
|
265
|
+
agentId: target.agentId,
|
|
266
|
+
sessionId,
|
|
267
|
+
entryId: delegationId ?? trigger.id,
|
|
268
|
+
triggerType: trigger.type,
|
|
269
|
+
human,
|
|
270
|
+
modelOverride: trigger.metadata?.modelOverride,
|
|
271
|
+
modelProvider: trigger.metadata?.modelProvider,
|
|
272
|
+
trigger,
|
|
273
|
+
onEvent,
|
|
274
|
+
message: compiledMessage,
|
|
275
|
+
onAdmitted: (delivery) => {
|
|
276
|
+
this.emitRuntime(RuntimeEvents.INBOX_ADMITTED, {
|
|
277
|
+
triggerId: trigger.id,
|
|
278
|
+
agentId: target.agentId,
|
|
279
|
+
sessionId,
|
|
280
|
+
delegationId,
|
|
281
|
+
delivery,
|
|
282
|
+
});
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
}
|
|
237
286
|
async dispatchToTarget(trigger, target, options) {
|
|
238
287
|
const agent = this.agents.get(target.agentId);
|
|
239
288
|
if (!agent) {
|
|
@@ -243,6 +292,14 @@ export class AgentRuntime {
|
|
|
243
292
|
...trigger,
|
|
244
293
|
sessionId: target.sessionId ?? trigger.sessionId,
|
|
245
294
|
});
|
|
295
|
+
// 单一 offer 点:活跃 Run 接收 = 不再开新 Run(原 Trigger 不 dispatch,
|
|
296
|
+
// Run 关闭未消费时由 inbox 按原 Trigger 重投)
|
|
297
|
+
const offerResult = this.offerToTarget(trigger, agent, sessionId, options?.onEvent);
|
|
298
|
+
if (offerResult === 'steered' || offerResult === 'duplicate') {
|
|
299
|
+
options?.onDelivery?.('steered');
|
|
300
|
+
return { status: 'accepted', agentId: agent.agentId, sessionId, delivery: 'steered' };
|
|
301
|
+
}
|
|
302
|
+
options?.onDelivery?.('run');
|
|
246
303
|
if (trigger.coalesceKey) {
|
|
247
304
|
this.emitRuntime(RuntimeEvents.RUN_SCHEDULED, {
|
|
248
305
|
triggerId: trigger.id,
|
|
@@ -291,15 +348,46 @@ export class AgentRuntime {
|
|
|
291
348
|
triggerType: triggers[0]?.type,
|
|
292
349
|
});
|
|
293
350
|
try {
|
|
351
|
+
// loop 终态如实镜像到 RUN_ENDED / DispatchResult:LLM 彻底失败以
|
|
352
|
+
// engine.error 事件收尾(不 throw),不得记成 completed——否则事故在
|
|
353
|
+
// 监控与 dispatch 结果里隐身(10-27 事故同族问题)
|
|
354
|
+
let endReason;
|
|
355
|
+
let endError;
|
|
294
356
|
for await (const event of agent.dispatcher.execute(request, controller.signal)) {
|
|
357
|
+
// 所有 Trigger(含后台 signal)共用一个 run-event 出口;调用方的
|
|
358
|
+
// onEvent 只承担该次 dispatch 的业务收尾,不再是后台 Run 的唯一可见通道。
|
|
359
|
+
this.emitRuntime(RuntimeEvents.RUN_EVENT, {
|
|
360
|
+
requestId: request.requestId,
|
|
361
|
+
triggerId: triggers[0]?.id,
|
|
362
|
+
triggerType: triggers[0]?.type,
|
|
363
|
+
// 路由键必带:gateway 按 sessionId 扇出 WS(缺则整条流式/终态链路静默落空)
|
|
364
|
+
agentId: agent.agentId,
|
|
365
|
+
sessionId,
|
|
366
|
+
event,
|
|
367
|
+
});
|
|
368
|
+
if (event.type === 'engine.end') {
|
|
369
|
+
const reason = event.data?.reason;
|
|
370
|
+
if (endReason !== 'error') {
|
|
371
|
+
// error 粘滞:合批(coalesce)里前一条 entry 失败、后一条成功时
|
|
372
|
+
// 不得被翻回 completed——任一 entry 失败即整体失败
|
|
373
|
+
endReason = reason === 'should_stop' || reason === 'aborted' ? reason : 'completed';
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else if (event.type === 'engine.error') {
|
|
377
|
+
endReason = 'error';
|
|
378
|
+
const rawError = event.data?.error;
|
|
379
|
+
// 与 runner 同口径取 message(自定义 dispatcher 直传 Error 时不留 "Error: " 前缀)
|
|
380
|
+
endError = rawError instanceof Error ? rawError.message : String(rawError ?? 'Unknown error');
|
|
381
|
+
}
|
|
295
382
|
try {
|
|
296
383
|
onEvent?.(event);
|
|
297
384
|
}
|
|
298
385
|
catch {
|
|
299
|
-
//
|
|
386
|
+
// 消费者(频道最终回复等)异常不中断 Run
|
|
300
387
|
}
|
|
301
388
|
}
|
|
302
|
-
|
|
389
|
+
// error 优先于 aborted:失败与中止同刻到达时,失败不得在监控里消失
|
|
390
|
+
if (controller.signal.aborted && endReason !== 'error') {
|
|
303
391
|
this.emitRuntime(RuntimeEvents.RUN_ENDED, {
|
|
304
392
|
requestId: request.requestId,
|
|
305
393
|
agentId: agent.agentId,
|
|
@@ -312,8 +400,12 @@ export class AgentRuntime {
|
|
|
312
400
|
requestId: request.requestId,
|
|
313
401
|
agentId: agent.agentId,
|
|
314
402
|
sessionId,
|
|
315
|
-
reason: 'completed',
|
|
403
|
+
reason: endReason ?? 'completed',
|
|
404
|
+
...(endError ? { error: endError } : {}),
|
|
316
405
|
});
|
|
406
|
+
if (endReason === 'error') {
|
|
407
|
+
return { status: 'failed', error: endError ?? 'unknown' };
|
|
408
|
+
}
|
|
317
409
|
return {
|
|
318
410
|
status: 'ran',
|
|
319
411
|
requestId: request.requestId,
|
|
@@ -95,6 +95,17 @@ export interface RunAuditRecord {
|
|
|
95
95
|
}
|
|
96
96
|
export interface RunDispatcher {
|
|
97
97
|
execute(req: RunRequest, signal?: AbortSignal): AsyncIterable<AgentEvent>;
|
|
98
|
+
/** 可选:活跃 Run 接续;缺省保持原 Trigger 排队语义。 */
|
|
99
|
+
inbox?: {
|
|
100
|
+
offer(signal: import('./inbox.js').SessionInboxSignal): import('./inbox.js').InboxOfferResult;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** dispatch 调用方选项 */
|
|
104
|
+
export interface DispatchOptions {
|
|
105
|
+
/** Run 事件流转发(频道回复捕获等);不变量 #6 */
|
|
106
|
+
onEvent?: (event: AgentEvent) => void;
|
|
107
|
+
/** 活跃 Run 接收即回执(早于 Run 结束);run = 走新 Run。 */
|
|
108
|
+
onDelivery?: (delivery: 'steered' | 'run') => void;
|
|
98
109
|
}
|
|
99
110
|
export interface RouteTarget {
|
|
100
111
|
agentId: string;
|
|
@@ -127,6 +138,12 @@ export type DispatchResult = {
|
|
|
127
138
|
requestId: string;
|
|
128
139
|
agentId: string;
|
|
129
140
|
sessionId: string;
|
|
141
|
+
} | {
|
|
142
|
+
/** 已交活跃 Run 在下一消息边界接续(不开新 Run)。 */
|
|
143
|
+
status: 'accepted';
|
|
144
|
+
agentId: string;
|
|
145
|
+
sessionId: string;
|
|
146
|
+
delivery: 'steered';
|
|
130
147
|
} | {
|
|
131
148
|
status: 'skipped';
|
|
132
149
|
reason: 'no_agent' | 'filtered' | 'runtime_stopped' | 'aborted';
|
|
@@ -159,6 +176,8 @@ export declare const RuntimeEvents: {
|
|
|
159
176
|
readonly RUN_FAILED: "runtime.run.failed";
|
|
160
177
|
readonly AGENT_SIGNAL_EMITTED: "runtime.agent_signal.emitted";
|
|
161
178
|
readonly TRIGGER_DROPPED: "runtime.trigger.dropped";
|
|
179
|
+
readonly RUN_EVENT: "runtime.run.event";
|
|
180
|
+
readonly INBOX_ADMITTED: "runtime.inbox.admitted";
|
|
162
181
|
};
|
|
163
182
|
export type RuntimeEventType = (typeof RuntimeEvents)[keyof typeof RuntimeEvents];
|
|
164
183
|
export interface RuntimeEvent {
|
|
@@ -18,7 +18,8 @@ import type { ModelProvider } from '@octopi-agent/core/interfaces/model-provider
|
|
|
18
18
|
import { Agent } from '../run/agent/index.js';
|
|
19
19
|
import type { ReliabilityConfig, ReliabilityHarness } from '../run/reliability/run-agent.js';
|
|
20
20
|
import type { ContextEngine, SummarizeFunction } from '../context/types.js';
|
|
21
|
-
import type { ErrorStrategy } from '@octopi-agent/core/interfaces/error-strategy.js';
|
|
21
|
+
import type { ErrorStrategy, ClassifiedError, ErrorAction, OverflowAction } from '@octopi-agent/core/interfaces/error-strategy.js';
|
|
22
|
+
import type { SecurityViolation, SecurityAction } from '@octopi-agent/core/security-guard.js';
|
|
22
23
|
import type { RunGuard } from '@octopi-agent/core/interfaces/run-guard.js';
|
|
23
24
|
import type { RunGuardConfig } from '../run/run-guard/default-run-guard.js';
|
|
24
25
|
import type { Observer } from '@octopi-agent/core/interfaces/observer.js';
|
|
@@ -31,6 +32,13 @@ import { SessionAwareRunner } from '../run/runner.js';
|
|
|
31
32
|
import type { SessionAwareRunnerConfig } from '../run/runner.js';
|
|
32
33
|
import type { McpClientFactory } from '../extension/plugin-ecosystem/mcp/manager.js';
|
|
33
34
|
import type { McpServerConfig, McpManager } from '../extension/plugin-ecosystem/mcp/types.js';
|
|
35
|
+
/** 默认错误策略(按原因分级重试 + 中止;上限见 MODEL_ERROR_MAX_ATTEMPTS) */
|
|
36
|
+
export declare class DefaultErrorStrategy implements ErrorStrategy {
|
|
37
|
+
onModelError(error: ClassifiedError, attempt: number): ErrorAction;
|
|
38
|
+
onToolError(error: ClassifiedError, _call: any): ErrorAction;
|
|
39
|
+
onContextOverflow(_tokenCount: number, _limit: number): OverflowAction;
|
|
40
|
+
onSecurityViolation(violation: SecurityViolation): SecurityAction;
|
|
41
|
+
}
|
|
34
42
|
/** 工具运行时上下文提供者 */
|
|
35
43
|
export interface ToolContextProvider {
|
|
36
44
|
get(): {
|
|
@@ -46,6 +54,12 @@ export interface ToolContextProvider {
|
|
|
46
54
|
pathContainment?: 'strict' | 'soft' | 'none';
|
|
47
55
|
confirmEffect?: import('@octopi-agent/core/types/tools.js').ToolExecutionContext['confirmEffect'];
|
|
48
56
|
effectChannel?: 'engine' | 'host-gate';
|
|
57
|
+
/** 提权策略(ops P3/P4,resolved;与 core ToolExecutionContext 同源) */
|
|
58
|
+
escalation?: import('@octopi-agent/core/types/tools.js').ToolExecutionContext['escalation'];
|
|
59
|
+
/** 危险命令减速带 resolved(§6;与 core ToolExecutionContext 同源) */
|
|
60
|
+
dangerousCommands?: import('@octopi-agent/core/types/tools.js').ToolExecutionContext['dangerousCommands'];
|
|
61
|
+
/** 参与者身份(SEC-2 / ops P5;与 core ToolExecutionContext 同源) */
|
|
62
|
+
sessionParticipants?: import('@octopi-agent/core/types/tools.js').ToolExecutionContext['sessionParticipants'];
|
|
49
63
|
processLimits?: boolean;
|
|
50
64
|
shellEscape?: 'deny' | 'confirm' | 'allow';
|
|
51
65
|
shellJournal?: 'snapshot' | 'off';
|
|
@@ -37,24 +37,30 @@ import { createProviderSummarize } from '../context/summarize.js';
|
|
|
37
37
|
import { createDefaultSystemPromptAssembler } from '../context/system-prompt-assembler.js';
|
|
38
38
|
import { SessionAwareRunner } from '../run/runner.js';
|
|
39
39
|
import { DefaultMcpManager } from '../extension/plugin-ecosystem/mcp/manager.js';
|
|
40
|
+
import { maxAttemptsFor } from '../run/reliability/model-error-policy.js';
|
|
40
41
|
// ── 默认实现 ──
|
|
41
|
-
/**
|
|
42
|
-
class DefaultErrorStrategy {
|
|
42
|
+
/** 默认错误策略(按原因分级重试 + 中止;上限见 MODEL_ERROR_MAX_ATTEMPTS) */
|
|
43
|
+
export class DefaultErrorStrategy {
|
|
43
44
|
onModelError(error, attempt) {
|
|
45
|
+
const maxAttempts = maxAttemptsFor(error.reason);
|
|
44
46
|
// ── 可重试错误:限流、超时、网络、服务端 ──
|
|
45
|
-
if (error.reason === 'rate_limit' && attempt <
|
|
47
|
+
if (error.reason === 'rate_limit' && attempt < maxAttempts) {
|
|
46
48
|
const delayMs = error.retryAfterMs ?? (attempt + 1) * 1000;
|
|
47
49
|
return { action: 'retry', delayMs };
|
|
48
50
|
}
|
|
49
|
-
if (error.reason === 'timeout' && attempt <
|
|
51
|
+
if (error.reason === 'timeout' && attempt < maxAttempts) {
|
|
50
52
|
return { action: 'retry', delayMs: (attempt + 1) * 1000 };
|
|
51
53
|
}
|
|
52
|
-
if (error.reason === 'network' && attempt <
|
|
54
|
+
if (error.reason === 'network' && attempt < maxAttempts) {
|
|
53
55
|
return { action: 'retry', delayMs: (attempt + 1) * 1500 };
|
|
54
56
|
}
|
|
55
|
-
if (error.reason === 'server' && attempt <
|
|
57
|
+
if (error.reason === 'server' && attempt < maxAttempts) {
|
|
56
58
|
return { action: 'retry', delayMs: (attempt + 1) * 2000 };
|
|
57
59
|
}
|
|
60
|
+
// ── content_filter:内容过滤拒答多为阈值型误杀,原样重试常过;不教模型换说法绕过 ──
|
|
61
|
+
if (error.reason === 'content_filter' && attempt < maxAttempts) {
|
|
62
|
+
return { action: 'retry', delayMs: (attempt + 1) * 1000 };
|
|
63
|
+
}
|
|
58
64
|
// ── context_length:由引擎尝试 compact,这里返回 abort 让引擎决定 ──
|
|
59
65
|
if (error.reason === 'context_length') {
|
|
60
66
|
return { action: 'abort', reason: `Context length exceeded: ${error.message}` };
|
|
@@ -116,11 +122,16 @@ class RuntimeToolContextProvider {
|
|
|
116
122
|
networkPolicy: scope.toolRuntime.networkPolicy,
|
|
117
123
|
requireCredentialBindings: scope.toolRuntime.requireCredentialBindings,
|
|
118
124
|
requireJail: scope.toolRuntime.requireJail,
|
|
125
|
+
disableJail: scope.toolRuntime.disableJail,
|
|
119
126
|
homeWrite: scope.toolRuntime.homeWrite,
|
|
120
127
|
noteSecurityDegraded: scope.toolRuntime.noteSecurityDegraded,
|
|
128
|
+
recordOpsEvent: scope.toolRuntime.recordOpsEvent,
|
|
121
129
|
pathContainment: scope.toolRuntime.pathContainment,
|
|
122
130
|
confirmEffect: scope.toolRuntime.confirmEffect,
|
|
123
131
|
effectChannel: scope.toolRuntime.effectChannel,
|
|
132
|
+
escalation: scope.toolRuntime.escalation,
|
|
133
|
+
dangerousCommands: scope.toolRuntime.dangerousCommands,
|
|
134
|
+
sessionParticipants: scope.toolRuntime.sessionParticipants,
|
|
124
135
|
processLimits: scope.toolRuntime.processLimits,
|
|
125
136
|
shellEscape: scope.toolRuntime.shellEscape,
|
|
126
137
|
shellJournal: scope.toolRuntime.shellJournal,
|
|
@@ -165,7 +176,7 @@ function convertToAgentTool(tool, contextProvider) {
|
|
|
165
176
|
description: tool.definition.description,
|
|
166
177
|
executionMode: tool.executionMode,
|
|
167
178
|
parameters: buildToolLlmParameters(tool.definition.parameters),
|
|
168
|
-
execute: async (toolCallId, args, signal) => {
|
|
179
|
+
execute: async (toolCallId, args, signal, onUpdate) => {
|
|
169
180
|
const startTime = Date.now();
|
|
170
181
|
try {
|
|
171
182
|
const context = contextProvider.get();
|
|
@@ -176,7 +187,12 @@ function convertToAgentTool(tool, contextProvider) {
|
|
|
176
187
|
const handlerArgs = prepareHandlerArgs(rawArgs, tool.definition);
|
|
177
188
|
const { runWithToolLimits, resolveToolTimeoutMs } = await import('../extension/execution-environment/resource-limits.js');
|
|
178
189
|
const processLimitsEnabled = context.processLimits !== false;
|
|
179
|
-
const result = await runWithToolLimits((sig) => tool.handler(handlerArgs, {
|
|
190
|
+
const result = await runWithToolLimits((sig) => tool.handler(handlerArgs, {
|
|
191
|
+
...context,
|
|
192
|
+
abortSignal: sig,
|
|
193
|
+
// 工具部分输出通道(展示用途);无转发器时不给工具假通道
|
|
194
|
+
...(onUpdate ? { onUpdate } : {}),
|
|
195
|
+
}), {
|
|
180
196
|
timeoutMs: processLimitsEnabled
|
|
181
197
|
? resolveToolTimeoutMs(tool.definition)
|
|
182
198
|
: undefined,
|
|
@@ -1334,7 +1350,11 @@ export class AgentBuilder {
|
|
|
1334
1350
|
const snap = getResolvedModel();
|
|
1335
1351
|
const provider = snap?.provider ?? this._model;
|
|
1336
1352
|
const model = snap?.modelName;
|
|
1337
|
-
return createProviderSummarize(provider, {
|
|
1353
|
+
return createProviderSummarize(provider, {
|
|
1354
|
+
model,
|
|
1355
|
+
// F8R-04:源头钳制与 gateway 对齐——自动路径同样受模型输出上限约束
|
|
1356
|
+
maxOutputTokens: snap?.maxOutputTokens,
|
|
1357
|
+
})(messages, opts);
|
|
1338
1358
|
}
|
|
1339
1359
|
: undefined);
|
|
1340
1360
|
agent.setConvertToLlm(async (messages) => {
|
|
@@ -1366,6 +1386,7 @@ export class AgentBuilder {
|
|
|
1366
1386
|
tools,
|
|
1367
1387
|
tokenBudget: contextWindow,
|
|
1368
1388
|
contextWindow,
|
|
1389
|
+
maxOutputTokens: snapshot?.maxOutputTokens,
|
|
1369
1390
|
compactTargetTokens: this._contextAssemblerConfig?.compactTargetTokens,
|
|
1370
1391
|
summarize: summarizeFn,
|
|
1371
1392
|
loadCompactState: () => agent.getSessionCompactState(sessionId, agentId),
|
|
@@ -1537,7 +1558,7 @@ export class AgentBuilder {
|
|
|
1537
1558
|
${toolList}
|
|
1538
1559
|
|
|
1539
1560
|
Tool selection (strict):
|
|
1540
|
-
1. Prefer a dedicated tool when one covers the job (file_read / file_list / file_write / file_edit / file_delete / file_mkdir / file_rename / file_copy / file_stat / file_rmdir / file_search / env_info, and any task-specific tool).
|
|
1561
|
+
1. Prefer a dedicated tool when one covers the job (file_read / file_list / file_write / file_edit / file_delete / file_mkdir / file_rename / file_copy / file_stat / file_rmdir / file_search / env_info / process_list, and any task-specific tool).
|
|
1541
1562
|
2. If a dedicated-tool call fails because your arguments were wrong (bad path, bad params), fix the call and retry that tool — do not switch to shell for a call you can fix.
|
|
1542
1563
|
3. Use shell only when: (a) no dedicated tool covers the operation, or (b) the dedicated tool is unavailable, or (c) it still fails after a correct retry and shell is the only remaining way to make progress.
|
|
1543
1564
|
4. Do not prefer shell over a working dedicated tool for the same job.
|
|
@@ -85,4 +85,9 @@ export interface BuildFromConfigOptions {
|
|
|
85
85
|
* 委派编排开关。缺省 = 只要有 agent 声明 `delegation` 策略就接线。
|
|
86
86
|
*/
|
|
87
87
|
delegation?: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* 宿主注入的 Runner 附加配置(浅合并进每个 agent)。
|
|
90
|
+
* ops P2:`securityIntegrity`(安全配置漂移守卫)与 `opsAuditPath` 走此通道。
|
|
91
|
+
*/
|
|
92
|
+
runnerConfig?: import('../run/runner.js').SessionAwareRunnerConfig;
|
|
88
93
|
}
|
|
@@ -131,6 +131,7 @@ export async function buildFromConfig(config, options) {
|
|
|
131
131
|
modelCallConfig: config.modelCall,
|
|
132
132
|
concurrencyConfig: config.concurrency,
|
|
133
133
|
observabilityConfig: config.observability,
|
|
134
|
+
runnerConfig: options?.runnerConfig,
|
|
134
135
|
});
|
|
135
136
|
agents.set(agentConfig.id, built);
|
|
136
137
|
}
|
|
@@ -171,6 +172,8 @@ export async function buildFromConfig(config, options) {
|
|
|
171
172
|
*/
|
|
172
173
|
async function buildAgent(agentConfig, shared) {
|
|
173
174
|
const builder = new AgentBuilder();
|
|
175
|
+
if (shared.runnerConfig)
|
|
176
|
+
builder.runnerConfig(shared.runnerConfig);
|
|
174
177
|
// 解析主模型 provider(宿主可在此包装 circuit breaker;config-bridge 用原始 provider)
|
|
175
178
|
const resolvedModel = resolveModelConfig(agentConfig.model, shared.flatModels);
|
|
176
179
|
const provider = shared.providers.get(resolvedModel.provider);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 安全配置指纹与漂移守卫(ops P2,`arch/ops-privileged-mode.md` §4.3 / §8.4 拍板)
|
|
3
|
+
*
|
|
4
|
+
* 「agent 可写自身配置」是自提权面:host 模式下 agent 改写 `octopi.json` 的安全字段
|
|
5
|
+
* 等于自我升档。边界(§8.4):安全字段**重启才生效**,运行中检测到漂移 → **拒新 run**
|
|
6
|
+
* + `ops.config.security_changed`,直至宿主(重启 / WebUI 署名通道)确认。
|
|
7
|
+
*
|
|
8
|
+
* 本模块只做「指纹 + 对比」;重读配置文件由宿主注入(引擎不越界做 IO 策略)。
|
|
9
|
+
*/
|
|
10
|
+
/** 安全字段指纹(锚定值 + 字段摘要) */
|
|
11
|
+
export interface SecurityConfigFingerprint {
|
|
12
|
+
/** sha256(canonicalJson(fields)) */
|
|
13
|
+
fingerprint: string;
|
|
14
|
+
/** 扁平字段摘要(`effectPolicy.jail` → 值),审计记录与 diff 用 */
|
|
15
|
+
fields: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
/** 指纹对比结果 */
|
|
18
|
+
export interface SecurityConfigDrift {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
/** 发生变化的字段名(指纹不同则至少一项) */
|
|
21
|
+
changed: string[];
|
|
22
|
+
/** 当前指纹(审计记录的「新摘要」) */
|
|
23
|
+
currentFingerprint: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 计算安全配置指纹(启动期锚定)。
|
|
27
|
+
*
|
|
28
|
+
* @param config - 已解析配置的安全相关切面(effectPolicy + security)
|
|
29
|
+
* @returns 指纹 + 字段摘要
|
|
30
|
+
*/
|
|
31
|
+
export declare function computeSecurityConfigFingerprint(config: {
|
|
32
|
+
effectPolicy?: Record<string, unknown>;
|
|
33
|
+
security?: Record<string, unknown>;
|
|
34
|
+
agents?: Array<Record<string, unknown>>;
|
|
35
|
+
plugins?: Record<string, unknown>;
|
|
36
|
+
}): SecurityConfigFingerprint;
|
|
37
|
+
/**
|
|
38
|
+
* 从原始 JSON 文本提取安全字段切面(漂移复核用;不走完整 schema 解析——
|
|
39
|
+
* 被篡改的配置可能根本过不了 schema,指纹对比必须先于校验)。
|
|
40
|
+
*
|
|
41
|
+
* 覆盖面(§8.4 + 评审补洞):顶层 effectPolicy / security、**agents[].effectPolicy**
|
|
42
|
+
* (每 agent 覆盖顶层,改单个 agent 的 jail 也必须算漂移)、**agents[].tools**(工具
|
|
43
|
+
* allow/deny)、**plugins.loadPaths**(插件加载路径)。
|
|
44
|
+
*
|
|
45
|
+
* @param jsonText - 配置文件全文
|
|
46
|
+
* @returns 安全字段切面
|
|
47
|
+
*/
|
|
48
|
+
export declare function extractSecurityConfigSlice(jsonText: string): {
|
|
49
|
+
effectPolicy: Record<string, unknown>;
|
|
50
|
+
security: Record<string, unknown>;
|
|
51
|
+
agents: Array<Record<string, unknown>>;
|
|
52
|
+
plugins: Record<string, unknown>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* 对比锚定指纹与当前指纹。
|
|
56
|
+
*
|
|
57
|
+
* @param anchor - 启动期指纹
|
|
58
|
+
* @param current - 当前指纹
|
|
59
|
+
* @returns 漂移结论(changed 为字段名列表)
|
|
60
|
+
*/
|
|
61
|
+
export declare function diffSecurityConfigFingerprint(anchor: SecurityConfigFingerprint, current: SecurityConfigFingerprint): SecurityConfigDrift;
|
|
62
|
+
/**
|
|
63
|
+
* 基于配置文件的漂移复核器(宿主注入 Runner 的 `securityIntegrity.recheck`)。
|
|
64
|
+
*
|
|
65
|
+
* @param options.configPath - 配置文件绝对路径
|
|
66
|
+
* @param options.anchor - 启动期锚定指纹
|
|
67
|
+
* @returns 复核函数:ok=false 时 changed 非空
|
|
68
|
+
*/
|
|
69
|
+
export declare function createFileSecurityIntegrity(options: {
|
|
70
|
+
configPath: string;
|
|
71
|
+
anchor: SecurityConfigFingerprint;
|
|
72
|
+
}): () => SecurityConfigDrift;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 安全配置指纹与漂移守卫(ops P2,`arch/ops-privileged-mode.md` §4.3 / §8.4 拍板)
|
|
3
|
+
*
|
|
4
|
+
* 「agent 可写自身配置」是自提权面:host 模式下 agent 改写 `octopi.json` 的安全字段
|
|
5
|
+
* 等于自我升档。边界(§8.4):安全字段**重启才生效**,运行中检测到漂移 → **拒新 run**
|
|
6
|
+
* + `ops.config.security_changed`,直至宿主(重启 / WebUI 署名通道)确认。
|
|
7
|
+
*
|
|
8
|
+
* 本模块只做「指纹 + 对比」;重读配置文件由宿主注入(引擎不越界做 IO 策略)。
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync } from 'node:fs';
|
|
11
|
+
import { createHash } from 'node:crypto';
|
|
12
|
+
/**
|
|
13
|
+
* 参与指纹的安全字段(§8.4:jail / escalation / effects / 审计开关 + 作用域面)。
|
|
14
|
+
* 键序无关;值走稳定序列化。
|
|
15
|
+
*/
|
|
16
|
+
const EFFECT_POLICY_SECURITY_KEYS = [
|
|
17
|
+
'mode',
|
|
18
|
+
'untrustedAdmission',
|
|
19
|
+
'pathContainment',
|
|
20
|
+
'shellEscape',
|
|
21
|
+
'jail',
|
|
22
|
+
'onJailUnavailable',
|
|
23
|
+
'privilegedAck',
|
|
24
|
+
'effects',
|
|
25
|
+
'toolIsolation',
|
|
26
|
+
'opsAudit',
|
|
27
|
+
'opsAuditPath',
|
|
28
|
+
// P5 审计外送也是安全面(换收口 = 换见证人)
|
|
29
|
+
'opsAuditExportPath',
|
|
30
|
+
'network',
|
|
31
|
+
'escalation',
|
|
32
|
+
// ops P4:危险命令减速带也是安全闸(改档 = 放松/收紧确认闸,须重启 + 宿主署名)
|
|
33
|
+
'dangerousCommands',
|
|
34
|
+
];
|
|
35
|
+
const SECURITY_CONFIG_SECURITY_KEYS = ['enforce', 'injectionSensitivity', 'allowedPaths'];
|
|
36
|
+
function pickFields(source, keys, prefix) {
|
|
37
|
+
const out = {};
|
|
38
|
+
for (const key of keys) {
|
|
39
|
+
if (source[key] !== undefined)
|
|
40
|
+
out[`${prefix}${key}`] = source[key];
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
function stableStringify(value) {
|
|
45
|
+
if (value === null || typeof value !== 'object')
|
|
46
|
+
return JSON.stringify(value) ?? 'null';
|
|
47
|
+
if (Array.isArray(value))
|
|
48
|
+
return `[${value.map((v) => stableStringify(v)).join(',')}]`;
|
|
49
|
+
const entries = Object.entries(value)
|
|
50
|
+
.filter(([, v]) => v !== undefined)
|
|
51
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
52
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(',')}}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 计算安全配置指纹(启动期锚定)。
|
|
56
|
+
*
|
|
57
|
+
* @param config - 已解析配置的安全相关切面(effectPolicy + security)
|
|
58
|
+
* @returns 指纹 + 字段摘要
|
|
59
|
+
*/
|
|
60
|
+
export function computeSecurityConfigFingerprint(config) {
|
|
61
|
+
const fields = {
|
|
62
|
+
...pickFields(config.effectPolicy ?? {}, EFFECT_POLICY_SECURITY_KEYS, 'effectPolicy.'),
|
|
63
|
+
...pickFields(config.security ?? {}, SECURITY_CONFIG_SECURITY_KEYS, 'security.'),
|
|
64
|
+
};
|
|
65
|
+
// per-agent 覆盖与工具面/插件加载路径都是安全面(§8.4 补洞)
|
|
66
|
+
if (config.agents !== undefined)
|
|
67
|
+
fields['agents'] = config.agents;
|
|
68
|
+
if (config.plugins !== undefined && Object.keys(config.plugins).length > 0) {
|
|
69
|
+
fields['plugins'] = pickFields(config.plugins, ['loadPaths'], 'plugins.');
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
fingerprint: createHash('sha256').update(stableStringify(fields)).digest('hex'),
|
|
73
|
+
fields,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 从原始 JSON 文本提取安全字段切面(漂移复核用;不走完整 schema 解析——
|
|
78
|
+
* 被篡改的配置可能根本过不了 schema,指纹对比必须先于校验)。
|
|
79
|
+
*
|
|
80
|
+
* 覆盖面(§8.4 + 评审补洞):顶层 effectPolicy / security、**agents[].effectPolicy**
|
|
81
|
+
* (每 agent 覆盖顶层,改单个 agent 的 jail 也必须算漂移)、**agents[].tools**(工具
|
|
82
|
+
* allow/deny)、**plugins.loadPaths**(插件加载路径)。
|
|
83
|
+
*
|
|
84
|
+
* @param jsonText - 配置文件全文
|
|
85
|
+
* @returns 安全字段切面
|
|
86
|
+
*/
|
|
87
|
+
export function extractSecurityConfigSlice(jsonText) {
|
|
88
|
+
const parsed = JSON.parse(jsonText);
|
|
89
|
+
const rawAgents = Array.isArray(parsed.agents) ? parsed.agents : [];
|
|
90
|
+
const agents = rawAgents.map((a) => {
|
|
91
|
+
const agent = (a ?? {});
|
|
92
|
+
return {
|
|
93
|
+
id: agent.id,
|
|
94
|
+
tools: agent.tools,
|
|
95
|
+
...pickFields((agent.effectPolicy ?? {}), EFFECT_POLICY_SECURITY_KEYS, 'effectPolicy.'),
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
effectPolicy: (parsed.effectPolicy ?? {}),
|
|
100
|
+
security: (parsed.security ?? {}),
|
|
101
|
+
agents,
|
|
102
|
+
plugins: (parsed.plugins ?? {}),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 对比锚定指纹与当前指纹。
|
|
107
|
+
*
|
|
108
|
+
* @param anchor - 启动期指纹
|
|
109
|
+
* @param current - 当前指纹
|
|
110
|
+
* @returns 漂移结论(changed 为字段名列表)
|
|
111
|
+
*/
|
|
112
|
+
export function diffSecurityConfigFingerprint(anchor, current) {
|
|
113
|
+
if (anchor.fingerprint === current.fingerprint) {
|
|
114
|
+
return { ok: true, changed: [], currentFingerprint: current.fingerprint };
|
|
115
|
+
}
|
|
116
|
+
const changed = new Set();
|
|
117
|
+
for (const key of new Set([...Object.keys(anchor.fields), ...Object.keys(current.fields)])) {
|
|
118
|
+
if (stableStringify(anchor.fields[key]) !== stableStringify(current.fields[key]))
|
|
119
|
+
changed.add(key);
|
|
120
|
+
}
|
|
121
|
+
return { ok: false, changed: [...changed].sort(), currentFingerprint: current.fingerprint };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 基于配置文件的漂移复核器(宿主注入 Runner 的 `securityIntegrity.recheck`)。
|
|
125
|
+
*
|
|
126
|
+
* @param options.configPath - 配置文件绝对路径
|
|
127
|
+
* @param options.anchor - 启动期锚定指纹
|
|
128
|
+
* @returns 复核函数:ok=false 时 changed 非空
|
|
129
|
+
*/
|
|
130
|
+
export function createFileSecurityIntegrity(options) {
|
|
131
|
+
return () => {
|
|
132
|
+
let slice;
|
|
133
|
+
try {
|
|
134
|
+
slice = extractSecurityConfigSlice(readFileSync(options.configPath, 'utf8'));
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
// 读不到/解析不了 = 无法证明未被篡改,按漂移对待(fail-closed)
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
changed: [`<config unreadable: ${err instanceof Error ? err.message : String(err)}>`],
|
|
141
|
+
currentFingerprint: '<unreadable>',
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return diffSecurityConfigFingerprint(options.anchor, computeSecurityConfigFingerprint(slice));
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -61,8 +61,9 @@ export function resolveContextEngine(config) {
|
|
|
61
61
|
// 缺省 type = default:只给旋钮(如 proactiveCompactRatio)也必须落到默认引擎
|
|
62
62
|
if (!config || config.type == null || config.type === 'default') {
|
|
63
63
|
return new DefaultContextEngine({
|
|
64
|
-
protectFirstN: config?.protectFirstN ??
|
|
64
|
+
protectFirstN: config?.protectFirstN ?? 5,
|
|
65
65
|
protectLastN: config?.protectLastN ?? 20,
|
|
66
|
+
summaryBudget: config?.summaryBudget,
|
|
66
67
|
proactiveCompactRatio: config?.proactiveCompactRatio ?? 0.6,
|
|
67
68
|
proactiveCooldownMs: config?.proactiveCooldownMs ?? 30_000,
|
|
68
69
|
historyAging: config?.historyAging,
|
|
@@ -220,7 +220,7 @@ export class ThinkExecutor {
|
|
|
220
220
|
.map(([key]) => key),
|
|
221
221
|
},
|
|
222
222
|
execute: t.handler
|
|
223
|
-
? async (toolCallId, args) => {
|
|
223
|
+
? async (toolCallId, args, _signal, onUpdate) => {
|
|
224
224
|
try {
|
|
225
225
|
const result = await t.handler(args, {
|
|
226
226
|
sessionId: 'subsystem',
|
|
@@ -228,6 +228,8 @@ export class ThinkExecutor {
|
|
|
228
228
|
messages: [],
|
|
229
229
|
fileEffects: createUnconfiguredFileEffects(),
|
|
230
230
|
memoryEffects: createUnconfiguredMemoryEffects(),
|
|
231
|
+
// 与 convertToAgentTool 同构:工具部分输出通道(think 子系统 loop 未挂 onToolUpdate 时为空)
|
|
232
|
+
...(onUpdate ? { onUpdate } : {}),
|
|
231
233
|
});
|
|
232
234
|
return {
|
|
233
235
|
toolCallId,
|