@octopi-agent/engine 0.90.11 → 0.98.4
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 +45 -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 +110 -4
- package/dist/config-schema/governance.d.ts +45 -1
- package/dist/config-schema/governance.js +187 -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 +26 -1
- package/dist/harness/agent/builder.js +85 -37
- 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 +138 -0
- package/dist/harness/agent/config-integrity.js +235 -0
- package/dist/harness/agent/wire-agent-builder.js +7 -1
- package/dist/harness/collaboration/autonomous-subsystem/index.d.ts +2 -2
- package/dist/harness/collaboration/autonomous-subsystem/index.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/loader.js +3 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +19 -2
- package/dist/harness/collaboration/autonomous-subsystem/think/completion-guard.d.ts +40 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/completion-guard.js +59 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +3 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/index.d.ts +2 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/index.js +1 -0
- package/dist/harness/collaboration/autonomous-subsystem/think/model-resolver.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +9 -0
- 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/capabilities/summary/resolver.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/resolver.js +4 -4
- package/dist/harness/context/capabilities/summary/types.d.ts +2 -2
- 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/layers.d.ts +2 -4
- package/dist/harness/context/layers.js +9 -11
- 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 +3 -1
- package/dist/harness/context/summarize.js +9 -4
- package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
- package/dist/harness/context/system-prompt-assembler.js +1 -0
- 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 +149 -8
- package/dist/harness/extension/execution-environment/effect-policy.js +274 -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 +204 -0
- package/dist/harness/extension/execution-environment/ops-audit.js +337 -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/execution-environment/undo-log.d.ts +6 -2
- package/dist/harness/extension/execution-environment/undo-log.js +31 -10
- 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 +70 -26
- package/dist/harness/extension/plugin-ecosystem/tools/document-tools.js +3 -9
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +3 -6
- package/dist/harness/extension/plugin-ecosystem/tools/file-ops.js +9 -5
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +9 -130
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +122 -14
- 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/package-tools.js +4 -13
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +25 -11
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +168 -62
- 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/default-risk-policy.d.ts +1 -0
- package/dist/harness/governance/security/default-risk-policy.js +1 -0
- package/dist/harness/governance/security/default-security-guard.js +9 -0
- package/dist/harness/governance/security/index.d.ts +1 -0
- package/dist/harness/governance/security/index.js +2 -0
- package/dist/harness/governance/security/risk-evaluator.js +13 -0
- package/dist/harness/governance/security/risk-types.d.ts +12 -0
- package/dist/harness/governance/security/shell-parser-fallback.d.ts +29 -0
- package/dist/harness/governance/security/shell-parser-fallback.js +410 -0
- package/dist/harness/governance/security/shell-parser-shared.d.ts +83 -0
- package/dist/harness/governance/security/shell-parser-shared.js +188 -0
- package/dist/harness/governance/security/shell-parser-treesitter.d.ts +52 -0
- package/dist/harness/governance/security/shell-parser-treesitter.js +370 -0
- package/dist/harness/governance/security/shell-parser.d.ts +23 -10
- package/dist/harness/governance/security/shell-parser.js +37 -496
- 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/security/vendor/README.md +24 -0
- package/dist/harness/governance/security/vendor/tree-sitter-bash.LICENSE.txt +21 -0
- package/dist/harness/governance/security/vendor/tree-sitter-bash.wasm +0 -0
- 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/index.d.ts +6 -2
- package/dist/harness/memory/index.js +4 -1
- package/dist/harness/memory/memory-events.d.ts +25 -0
- package/dist/harness/memory/memory-events.js +38 -0
- package/dist/harness/memory/sqlite/agent-db.js +15 -0
- package/dist/harness/memory/sqlite/keyword-search.d.ts +1 -0
- package/dist/harness/memory/sqlite/keyword-search.js +4 -3
- package/dist/harness/memory/sqlite/wisdom-store.d.ts +7 -2
- package/dist/harness/memory/sqlite/wisdom-store.js +51 -2
- package/dist/harness/memory/steward-policy.d.ts +3 -0
- package/dist/harness/memory/steward-policy.js +1 -1
- package/dist/harness/memory/types.d.ts +41 -3
- package/dist/harness/memory/wisdom-attribution.d.ts +136 -0
- package/dist/harness/memory/wisdom-attribution.js +287 -0
- package/dist/harness/memory/wisdom-events.d.ts +37 -0
- package/dist/harness/memory/wisdom-events.js +61 -0
- package/dist/harness/memory/wisdom-formation.d.ts +20 -1
- package/dist/harness/memory/wisdom-formation.js +55 -21
- package/dist/harness/memory/wisdom.d.ts +7 -2
- package/dist/harness/memory/wisdom.js +40 -2
- 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 +35 -0
- package/dist/harness/run/runner.d.ts +141 -2
- package/dist/harness/run/runner.js +592 -42
- 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/harness/shared/paths/glob.d.ts +52 -0
- package/dist/harness/shared/paths/glob.js +218 -0
- package/dist/harness/shared/paths/index.d.ts +84 -0
- package/dist/harness/shared/paths/index.js +179 -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/config.yaml +4 -2
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +12 -20
- package/dist/subsystems/memory-steward/backfill/config.yaml +4 -2
- package/dist/subsystems/memory-steward/backfill/handler.js +44 -52
- package/dist/subsystems/wisdom-steward/attribute/SUBSYSTEM.md +24 -0
- package/dist/subsystems/wisdom-steward/attribute/config.yaml +60 -0
- package/dist/subsystems/wisdom-steward/attribute/handler.d.ts +23 -0
- package/dist/subsystems/wisdom-steward/attribute/handler.js +167 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.js +3 -1
- package/dist/subsystems/wisdom-steward/formation/config.yaml +12 -5
- package/dist/subsystems/wisdom-steward/formation/handler.js +12 -18
- package/dist/subsystems/wisdom-steward/govern/config.yaml +1 -1
- package/dist/subsystems/wisdom-steward/govern/handler.js +4 -0
- package/package.json +8 -2
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
* effectPolicy 中**不进热应用**的子字段(§4.3.1:需重启 + 宿主确认)。
|
|
27
|
+
*
|
|
28
|
+
* 判据是 §4.3.1 **明列**的安全闸 + 注释明确「须重启 + 宿主署名」的字段,**不是**整个
|
|
29
|
+
* 指纹范围(EFFECT_POLICY_SECURITY_KEYS)——指纹范围只回答「哪些字段变化算漂移」,
|
|
30
|
+
* 热应用禁区回答「哪些字段改了必须重启确认」,两者不可混用(把 shellEscape 等一并
|
|
31
|
+
* 划入禁区会误拒 WebUI 的合法合并编辑)。
|
|
32
|
+
*
|
|
33
|
+
* `mode` 允许热改(WebUI「安全组」四选一);`shellEscape` / `untrustedAdmission` /
|
|
34
|
+
* `pathContainment` / `toolIsolation` / `network` 是普通配置旋钮,允许热改(仍在指纹内,
|
|
35
|
+
* 改了会重锚)。ApplyEngine 据此拒绝禁区字段(评审 M2 补全此前缺失的拒绝逻辑)。
|
|
36
|
+
*/
|
|
37
|
+
export declare const EFFECT_POLICY_HOT_BLOCKED_KEYS: readonly string[];
|
|
38
|
+
/**
|
|
39
|
+
* 计算安全配置指纹(启动期锚定)。
|
|
40
|
+
*
|
|
41
|
+
* @param config - 已解析配置的安全相关切面(effectPolicy + security)
|
|
42
|
+
* @returns 指纹 + 字段摘要
|
|
43
|
+
*/
|
|
44
|
+
export declare function computeSecurityConfigFingerprint(config: {
|
|
45
|
+
effectPolicy?: Record<string, unknown>;
|
|
46
|
+
security?: Record<string, unknown>;
|
|
47
|
+
agents?: Array<Record<string, unknown>>;
|
|
48
|
+
plugins?: Record<string, unknown>;
|
|
49
|
+
}): SecurityConfigFingerprint;
|
|
50
|
+
/**
|
|
51
|
+
* 从原始 JSON 文本提取安全字段切面(漂移复核用;不走完整 schema 解析——
|
|
52
|
+
* 被篡改的配置可能根本过不了 schema,指纹对比必须先于校验)。
|
|
53
|
+
*
|
|
54
|
+
* 覆盖面(§8.4 + 评审补洞):顶层 effectPolicy / security、**agents[].effectPolicy**
|
|
55
|
+
* (每 agent 覆盖顶层,改单个 agent 的 jail 也必须算漂移)、**agents[].tools**(工具
|
|
56
|
+
* allow/deny)、**plugins.loadPaths**(插件加载路径)。
|
|
57
|
+
*
|
|
58
|
+
* @param jsonText - 配置文件全文
|
|
59
|
+
* @returns 安全字段切面
|
|
60
|
+
*/
|
|
61
|
+
export declare function extractSecurityConfigSlice(jsonText: string): {
|
|
62
|
+
effectPolicy: Record<string, unknown>;
|
|
63
|
+
security: Record<string, unknown>;
|
|
64
|
+
agents: Array<Record<string, unknown>>;
|
|
65
|
+
plugins: Record<string, unknown>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* 对比锚定指纹与当前指纹。
|
|
69
|
+
*
|
|
70
|
+
* @param anchor - 启动期指纹
|
|
71
|
+
* @param current - 当前指纹
|
|
72
|
+
* @returns 漂移结论(changed 为字段名列表)
|
|
73
|
+
*/
|
|
74
|
+
export declare function diffSecurityConfigFingerprint(anchor: SecurityConfigFingerprint, current: SecurityConfigFingerprint): SecurityConfigDrift;
|
|
75
|
+
/**
|
|
76
|
+
* 基于配置文件的漂移复核器(宿主注入 Runner 的 `securityIntegrity.recheck`)。
|
|
77
|
+
*
|
|
78
|
+
* @param options.configPath - 配置文件绝对路径
|
|
79
|
+
* @param options.anchor - 启动期锚定指纹
|
|
80
|
+
* @returns 复核函数:ok=false 时 changed 非空
|
|
81
|
+
*/
|
|
82
|
+
export declare function createFileSecurityIntegrity(options: {
|
|
83
|
+
configPath: string;
|
|
84
|
+
anchor: SecurityConfigFingerprint;
|
|
85
|
+
}): () => SecurityConfigDrift;
|
|
86
|
+
/**
|
|
87
|
+
* 可重锚定守卫(ops P2 §8.4「WebUI 署名通道」落地):启动锚定 → 每 run 复核 →
|
|
88
|
+
* 受控写通道写盘成功后 {@link SecurityIntegrityGuard.reattest} 重新锚定。
|
|
89
|
+
*
|
|
90
|
+
* 威胁模型边界:reattest 的调用方是宿主的受控写通道(console 管理面,带鉴权),
|
|
91
|
+
* 等价于人工重启的署名确认;agent 绕过通道直改文件依然被 recheck 判漂移(拒新 run 不变)。
|
|
92
|
+
*/
|
|
93
|
+
export interface SecurityIntegrityGuard {
|
|
94
|
+
/** 当前锚定指纹(活视图:reattest 后更新;漂移审计的「旧摘要」读它) */
|
|
95
|
+
readonly fingerprint: string;
|
|
96
|
+
/** 复核当前安全配置(读不到/解析失败按漂移对待,fail-closed) */
|
|
97
|
+
recheck(): SecurityConfigDrift;
|
|
98
|
+
/**
|
|
99
|
+
* 受控写通道写盘成功后重新锚定。
|
|
100
|
+
*
|
|
101
|
+
* 两道校验(缺一不可,评审 M1):
|
|
102
|
+
* - `expectHash`:写盘方给出的写后原文 hash。rename 与重锚之间若有第三方写入
|
|
103
|
+
* (TOCTOU),hash 不符 → 拒绝重锚。
|
|
104
|
+
* - `allowedChanged`:允许被本次写改变的顶层安全字段。changed 超出白名单 =
|
|
105
|
+
* 既存篡改或越权写(正是 §8.4 要防的自提权面)→ 拒绝重锚,防止一次无关的
|
|
106
|
+
* console 写把已存在的漂移「洗白」。
|
|
107
|
+
*
|
|
108
|
+
* 两者任一不符即抛错:调用方降级为 warning,守卫维持漂移态(fail-closed),
|
|
109
|
+
* 直至宿主重启采纳盘上配置。
|
|
110
|
+
*
|
|
111
|
+
* @param options - 校验参数(见上)
|
|
112
|
+
* @returns 旧→新指纹与变化字段
|
|
113
|
+
* @throws 校验不符 / 盘上文件读不到
|
|
114
|
+
*/
|
|
115
|
+
reattest(options?: {
|
|
116
|
+
/** 写后原文 hash(`computeConfigHash`);不符 = TOCTOU,拒绝重锚 */
|
|
117
|
+
expectHash?: string;
|
|
118
|
+
/**
|
|
119
|
+
* 允许变化的顶层安全字段(`'agents'` | `'effectPolicy'` | `'security'` | `'plugins'`)。
|
|
120
|
+
* 省略 = 不限(仅宿主显式全量确认,如重启采纳盘上配置)
|
|
121
|
+
*/
|
|
122
|
+
allowedChanged?: string[];
|
|
123
|
+
}): {
|
|
124
|
+
previousFingerprint: string;
|
|
125
|
+
fingerprint: string;
|
|
126
|
+
changed: string[];
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 基于配置文件的可重锚定守卫。
|
|
131
|
+
*
|
|
132
|
+
* @param options.configPath - 配置文件绝对路径
|
|
133
|
+
* @returns 守卫(锚定与复核同口径:都取 raw 文件安全切面)
|
|
134
|
+
* @throws 启动期文件读不到/解析失败(fail-fast,与锚定即拒绝启动的既有语义一致)
|
|
135
|
+
*/
|
|
136
|
+
export declare function createSecurityIntegrityGuard(options: {
|
|
137
|
+
configPath: string;
|
|
138
|
+
}): SecurityIntegrityGuard;
|
|
@@ -0,0 +1,235 @@
|
|
|
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
|
+
import { computeConfigHash } from '../../config-admin/patch.js';
|
|
13
|
+
/**
|
|
14
|
+
* 参与指纹的安全字段(§8.4:jail / escalation / effects / 审计开关 + 作用域面)。
|
|
15
|
+
* 键序无关;值走稳定序列化。
|
|
16
|
+
*/
|
|
17
|
+
const EFFECT_POLICY_SECURITY_KEYS = [
|
|
18
|
+
'mode',
|
|
19
|
+
'untrustedAdmission',
|
|
20
|
+
'pathContainment',
|
|
21
|
+
'shellEscape',
|
|
22
|
+
'jail',
|
|
23
|
+
'onJailUnavailable',
|
|
24
|
+
'privilegedAck',
|
|
25
|
+
'effects',
|
|
26
|
+
'toolIsolation',
|
|
27
|
+
'opsAudit',
|
|
28
|
+
'opsAuditPath',
|
|
29
|
+
// P5 审计外送也是安全面(换收口 = 换见证人)
|
|
30
|
+
'opsAuditExportPath',
|
|
31
|
+
'network',
|
|
32
|
+
'escalation',
|
|
33
|
+
// ops P4:危险命令减速带也是安全闸(改档 = 放松/收紧确认闸,须重启 + 宿主署名)
|
|
34
|
+
'dangerousCommands',
|
|
35
|
+
];
|
|
36
|
+
const SECURITY_CONFIG_SECURITY_KEYS = ['enforce', 'injectionSensitivity', 'allowedPaths'];
|
|
37
|
+
/**
|
|
38
|
+
* effectPolicy 中**不进热应用**的子字段(§4.3.1:需重启 + 宿主确认)。
|
|
39
|
+
*
|
|
40
|
+
* 判据是 §4.3.1 **明列**的安全闸 + 注释明确「须重启 + 宿主署名」的字段,**不是**整个
|
|
41
|
+
* 指纹范围(EFFECT_POLICY_SECURITY_KEYS)——指纹范围只回答「哪些字段变化算漂移」,
|
|
42
|
+
* 热应用禁区回答「哪些字段改了必须重启确认」,两者不可混用(把 shellEscape 等一并
|
|
43
|
+
* 划入禁区会误拒 WebUI 的合法合并编辑)。
|
|
44
|
+
*
|
|
45
|
+
* `mode` 允许热改(WebUI「安全组」四选一);`shellEscape` / `untrustedAdmission` /
|
|
46
|
+
* `pathContainment` / `toolIsolation` / `network` 是普通配置旋钮,允许热改(仍在指纹内,
|
|
47
|
+
* 改了会重锚)。ApplyEngine 据此拒绝禁区字段(评审 M2 补全此前缺失的拒绝逻辑)。
|
|
48
|
+
*/
|
|
49
|
+
export const EFFECT_POLICY_HOT_BLOCKED_KEYS = [
|
|
50
|
+
// §4.3.1 明列
|
|
51
|
+
'jail',
|
|
52
|
+
'onJailUnavailable',
|
|
53
|
+
'privilegedAck',
|
|
54
|
+
'effects',
|
|
55
|
+
'escalation',
|
|
56
|
+
// §4.3.1「审计开关」;P5 审计外送 = 换见证人
|
|
57
|
+
'opsAudit',
|
|
58
|
+
'opsAuditPath',
|
|
59
|
+
'opsAuditExportPath',
|
|
60
|
+
// ops P4 注释明说「危险命令减速带…须重启 + 宿主署名」
|
|
61
|
+
'dangerousCommands',
|
|
62
|
+
];
|
|
63
|
+
function pickFields(source, keys, prefix) {
|
|
64
|
+
const out = {};
|
|
65
|
+
for (const key of keys) {
|
|
66
|
+
if (source[key] !== undefined)
|
|
67
|
+
out[`${prefix}${key}`] = source[key];
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function stableStringify(value) {
|
|
72
|
+
if (value === null || typeof value !== 'object')
|
|
73
|
+
return JSON.stringify(value) ?? 'null';
|
|
74
|
+
if (Array.isArray(value))
|
|
75
|
+
return `[${value.map((v) => stableStringify(v)).join(',')}]`;
|
|
76
|
+
const entries = Object.entries(value)
|
|
77
|
+
.filter(([, v]) => v !== undefined)
|
|
78
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
79
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(',')}}`;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 计算安全配置指纹(启动期锚定)。
|
|
83
|
+
*
|
|
84
|
+
* @param config - 已解析配置的安全相关切面(effectPolicy + security)
|
|
85
|
+
* @returns 指纹 + 字段摘要
|
|
86
|
+
*/
|
|
87
|
+
export function computeSecurityConfigFingerprint(config) {
|
|
88
|
+
const fields = {
|
|
89
|
+
...pickFields(config.effectPolicy ?? {}, EFFECT_POLICY_SECURITY_KEYS, 'effectPolicy.'),
|
|
90
|
+
...pickFields(config.security ?? {}, SECURITY_CONFIG_SECURITY_KEYS, 'security.'),
|
|
91
|
+
};
|
|
92
|
+
// per-agent 覆盖与工具面/插件加载路径都是安全面(§8.4 补洞)
|
|
93
|
+
if (config.agents !== undefined)
|
|
94
|
+
fields['agents'] = config.agents;
|
|
95
|
+
if (config.plugins !== undefined && Object.keys(config.plugins).length > 0) {
|
|
96
|
+
fields['plugins'] = pickFields(config.plugins, ['loadPaths'], 'plugins.');
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
fingerprint: createHash('sha256').update(stableStringify(fields)).digest('hex'),
|
|
100
|
+
fields,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 从原始 JSON 文本提取安全字段切面(漂移复核用;不走完整 schema 解析——
|
|
105
|
+
* 被篡改的配置可能根本过不了 schema,指纹对比必须先于校验)。
|
|
106
|
+
*
|
|
107
|
+
* 覆盖面(§8.4 + 评审补洞):顶层 effectPolicy / security、**agents[].effectPolicy**
|
|
108
|
+
* (每 agent 覆盖顶层,改单个 agent 的 jail 也必须算漂移)、**agents[].tools**(工具
|
|
109
|
+
* allow/deny)、**plugins.loadPaths**(插件加载路径)。
|
|
110
|
+
*
|
|
111
|
+
* @param jsonText - 配置文件全文
|
|
112
|
+
* @returns 安全字段切面
|
|
113
|
+
*/
|
|
114
|
+
export function extractSecurityConfigSlice(jsonText) {
|
|
115
|
+
const parsed = JSON.parse(jsonText);
|
|
116
|
+
const rawAgents = Array.isArray(parsed.agents) ? parsed.agents : [];
|
|
117
|
+
const agents = rawAgents.map((a) => {
|
|
118
|
+
const agent = (a ?? {});
|
|
119
|
+
return {
|
|
120
|
+
id: agent.id,
|
|
121
|
+
tools: agent.tools,
|
|
122
|
+
...pickFields((agent.effectPolicy ?? {}), EFFECT_POLICY_SECURITY_KEYS, 'effectPolicy.'),
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
effectPolicy: (parsed.effectPolicy ?? {}),
|
|
127
|
+
security: (parsed.security ?? {}),
|
|
128
|
+
agents,
|
|
129
|
+
plugins: (parsed.plugins ?? {}),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 对比锚定指纹与当前指纹。
|
|
134
|
+
*
|
|
135
|
+
* @param anchor - 启动期指纹
|
|
136
|
+
* @param current - 当前指纹
|
|
137
|
+
* @returns 漂移结论(changed 为字段名列表)
|
|
138
|
+
*/
|
|
139
|
+
export function diffSecurityConfigFingerprint(anchor, current) {
|
|
140
|
+
if (anchor.fingerprint === current.fingerprint) {
|
|
141
|
+
return { ok: true, changed: [], currentFingerprint: current.fingerprint };
|
|
142
|
+
}
|
|
143
|
+
const changed = new Set();
|
|
144
|
+
for (const key of new Set([...Object.keys(anchor.fields), ...Object.keys(current.fields)])) {
|
|
145
|
+
if (stableStringify(anchor.fields[key]) !== stableStringify(current.fields[key]))
|
|
146
|
+
changed.add(key);
|
|
147
|
+
}
|
|
148
|
+
return { ok: false, changed: [...changed].sort(), currentFingerprint: current.fingerprint };
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 基于配置文件的漂移复核器(宿主注入 Runner 的 `securityIntegrity.recheck`)。
|
|
152
|
+
*
|
|
153
|
+
* @param options.configPath - 配置文件绝对路径
|
|
154
|
+
* @param options.anchor - 启动期锚定指纹
|
|
155
|
+
* @returns 复核函数:ok=false 时 changed 非空
|
|
156
|
+
*/
|
|
157
|
+
export function createFileSecurityIntegrity(options) {
|
|
158
|
+
return () => {
|
|
159
|
+
let slice;
|
|
160
|
+
try {
|
|
161
|
+
slice = extractSecurityConfigSlice(readFileSync(options.configPath, 'utf8'));
|
|
162
|
+
}
|
|
163
|
+
catch (err) {
|
|
164
|
+
// 读不到/解析不了 = 无法证明未被篡改,按漂移对待(fail-closed)
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
changed: [`<config unreadable: ${err instanceof Error ? err.message : String(err)}>`],
|
|
168
|
+
currentFingerprint: '<unreadable>',
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return diffSecurityConfigFingerprint(options.anchor, computeSecurityConfigFingerprint(slice));
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 基于配置文件的可重锚定守卫。
|
|
176
|
+
*
|
|
177
|
+
* @param options.configPath - 配置文件绝对路径
|
|
178
|
+
* @returns 守卫(锚定与复核同口径:都取 raw 文件安全切面)
|
|
179
|
+
* @throws 启动期文件读不到/解析失败(fail-fast,与锚定即拒绝启动的既有语义一致)
|
|
180
|
+
*/
|
|
181
|
+
export function createSecurityIntegrityGuard(options) {
|
|
182
|
+
const readSlice = () => {
|
|
183
|
+
const text = readFileSync(options.configPath, 'utf8');
|
|
184
|
+
return { text, slice: extractSecurityConfigSlice(text) };
|
|
185
|
+
};
|
|
186
|
+
let anchor = computeSecurityConfigFingerprint(readSlice().slice);
|
|
187
|
+
return {
|
|
188
|
+
get fingerprint() {
|
|
189
|
+
return anchor.fingerprint;
|
|
190
|
+
},
|
|
191
|
+
recheck() {
|
|
192
|
+
let slice;
|
|
193
|
+
try {
|
|
194
|
+
slice = extractSecurityConfigSlice(readFileSync(options.configPath, 'utf8'));
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
// 读不到/解析不了 = 无法证明未被篡改,按漂移对待(fail-closed)
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
changed: [`<config unreadable: ${err instanceof Error ? err.message : String(err)}>`],
|
|
201
|
+
currentFingerprint: '<unreadable>',
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return diffSecurityConfigFingerprint(anchor, computeSecurityConfigFingerprint(slice));
|
|
205
|
+
},
|
|
206
|
+
reattest(reattestOptions) {
|
|
207
|
+
// 刚写盘的文件读不到/解析不了 = 内部异常:抛给调用方降级处理(锚不换,fail-closed 保持)
|
|
208
|
+
const { text, slice } = readSlice();
|
|
209
|
+
// 校验一(TOCTOU):写后 hash 与盘上现状不符 = 重锚前有第三方写入
|
|
210
|
+
if (reattestOptions?.expectHash !== undefined) {
|
|
211
|
+
const actualHash = computeConfigHash(text);
|
|
212
|
+
if (actualHash !== reattestOptions.expectHash) {
|
|
213
|
+
throw new Error(`security re-attest refused: config changed on disk since the console write (expected ${reattestOptions.expectHash.slice(0, 8)}, got ${actualHash.slice(0, 8)}); host restart attests instead`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const current = computeSecurityConfigFingerprint(slice);
|
|
217
|
+
const diff = diffSecurityConfigFingerprint(anchor, current);
|
|
218
|
+
// 校验二(防白洗):本次写只应改变它声明的字段;其余安全字段变化 = 既存篡改/越权
|
|
219
|
+
if (reattestOptions?.allowedChanged) {
|
|
220
|
+
const allowed = new Set(reattestOptions.allowedChanged);
|
|
221
|
+
const unexpected = diff.changed.filter((field) => !allowed.has(field.split('.')[0] ?? ''));
|
|
222
|
+
if (unexpected.length > 0) {
|
|
223
|
+
throw new Error(`security re-attest refused: security fields outside this write's scope changed (${unexpected.join(', ')}); refusing to attest them — host restart required`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const previous = anchor;
|
|
227
|
+
anchor = current;
|
|
228
|
+
return {
|
|
229
|
+
previousFingerprint: previous.fingerprint,
|
|
230
|
+
fingerprint: current.fingerprint,
|
|
231
|
+
changed: diff.changed,
|
|
232
|
+
};
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
@@ -18,6 +18,7 @@ import { getOctopiHome } from '../../paths.js';
|
|
|
18
18
|
import { DefaultRunGuard, } from '../run/run-guard/default-run-guard.js';
|
|
19
19
|
import { DefaultContextEngine } from '../context/default-context-engine.js';
|
|
20
20
|
import { DefaultBudgetAllocator } from '../context/budget-allocator.js';
|
|
21
|
+
import { initShellParser } from '../governance/security/shell-parser.js';
|
|
21
22
|
// ── 配置解析(唯一实现;供 wire 与公开导出共用) ──
|
|
22
23
|
/**
|
|
23
24
|
* 解析子系统审计目录(docs/autonomous-subsystem.md §10)
|
|
@@ -61,8 +62,9 @@ export function resolveContextEngine(config) {
|
|
|
61
62
|
// 缺省 type = default:只给旋钮(如 proactiveCompactRatio)也必须落到默认引擎
|
|
62
63
|
if (!config || config.type == null || config.type === 'default') {
|
|
63
64
|
return new DefaultContextEngine({
|
|
64
|
-
protectFirstN: config?.protectFirstN ??
|
|
65
|
+
protectFirstN: config?.protectFirstN ?? 5,
|
|
65
66
|
protectLastN: config?.protectLastN ?? 20,
|
|
67
|
+
summaryBudget: config?.summaryBudget,
|
|
66
68
|
proactiveCompactRatio: config?.proactiveCompactRatio ?? 0.6,
|
|
67
69
|
proactiveCooldownMs: config?.proactiveCooldownMs ?? 30_000,
|
|
68
70
|
historyAging: config?.historyAging,
|
|
@@ -122,6 +124,8 @@ export function resolveRunGuard(config, providers) {
|
|
|
122
124
|
* @param spec - 标准组装输入
|
|
123
125
|
*/
|
|
124
126
|
export async function wireAgentBuilder(builder, spec) {
|
|
127
|
+
// ── Shell 解析引擎(安全风险评估依赖;失败自动降级 legacy,不阻断组装) ──
|
|
128
|
+
await initShellParser();
|
|
125
129
|
// ── Model ──
|
|
126
130
|
builder.model(spec.model);
|
|
127
131
|
// ── Model call watchdog ──
|
|
@@ -345,6 +349,8 @@ async function wireContextStores(builder, input) {
|
|
|
345
349
|
const wis = input.wisdomConfig;
|
|
346
350
|
if (wis?.enabled !== false) {
|
|
347
351
|
builder.wisdomStore(new SqliteWisdomStore(db, { hardCap: wis?.capacity?.hardCap }));
|
|
352
|
+
if (wis?.injection)
|
|
353
|
+
builder.wisdomInjection(wis.injection);
|
|
348
354
|
}
|
|
349
355
|
const cog = input.cognitionConfig;
|
|
350
356
|
if (cog?.enabled !== false) {
|
|
@@ -16,8 +16,8 @@ export type { SharedDeps, SubsystemRuntimeConfig, SubsystemTriggerResult, Subsys
|
|
|
16
16
|
export { SenseEngine, MetricsStore, SessionLifecycleBridge } from './sense/index.js';
|
|
17
17
|
export { buildAgentInput } from './sense/input-builder.js';
|
|
18
18
|
export { ThinkExecutor, ModelResolver } from './think/index.js';
|
|
19
|
-
export { createSubsystemLLMPort, shouldFallbackModel, DEP_LLM_PORT, DEP_SUBSYSTEM_PROMPT, DEP_RESOLVED_MODEL, DEP_RESOLVED_MODELS, } from './think/index.js';
|
|
20
|
-
export type { SubsystemLLMPort, SubsystemLLMPortChatRequest, CreateSubsystemLLMPortOptions, } from './think/index.js';
|
|
19
|
+
export { createSubsystemLLMPort, shouldFallbackModel, DEP_LLM_PORT, DEP_SUBSYSTEM_PROMPT, DEP_RESOLVED_MODEL, DEP_RESOLVED_MODELS, chatForCompleteOutput, } from './think/index.js';
|
|
20
|
+
export type { SubsystemLLMPort, SubsystemLLMPortChatRequest, CreateSubsystemLLMPortOptions, CompletionGuardFailure, CompletionGuardRequest, CompletionGuardResult, } from './think/index.js';
|
|
21
21
|
export { SignalBus } from './signal/index.js';
|
|
22
22
|
export { SubsystemSessionManager, parseTTL } from './session/index.js';
|
|
23
23
|
export { AuditWriter, AuditReader } from './audit/index.js';
|
|
@@ -17,7 +17,7 @@ export { SenseEngine, MetricsStore, SessionLifecycleBridge } from './sense/index
|
|
|
17
17
|
export { buildAgentInput } from './sense/input-builder.js';
|
|
18
18
|
// ── Think ──
|
|
19
19
|
export { ThinkExecutor, ModelResolver } from './think/index.js';
|
|
20
|
-
export { createSubsystemLLMPort, shouldFallbackModel, DEP_LLM_PORT, DEP_SUBSYSTEM_PROMPT, DEP_RESOLVED_MODEL, DEP_RESOLVED_MODELS, } from './think/index.js';
|
|
20
|
+
export { createSubsystemLLMPort, shouldFallbackModel, DEP_LLM_PORT, DEP_SUBSYSTEM_PROMPT, DEP_RESOLVED_MODEL, DEP_RESOLVED_MODELS, chatForCompleteOutput, } from './think/index.js';
|
|
21
21
|
// ── Signal ──
|
|
22
22
|
export { SignalBus } from './signal/index.js';
|
|
23
23
|
// ── Session ──
|
|
@@ -277,9 +277,12 @@ async function buildSpec(config, systemPrompt, source, dirPath) {
|
|
|
277
277
|
events: filterRaw.events,
|
|
278
278
|
condition: filterRaw.condition,
|
|
279
279
|
conditionRef: filterRaw.conditionRef,
|
|
280
|
+
minEvents: filterRaw.minEvents,
|
|
280
281
|
emits: filterRaw.emits,
|
|
281
282
|
} : undefined,
|
|
282
283
|
interval: senseRaw.interval,
|
|
284
|
+
runOnStart: senseRaw.runOnStart,
|
|
285
|
+
cooldownMs: senseRaw.cooldownMs,
|
|
283
286
|
isolation: (senseRaw.isolation ?? 'structured'),
|
|
284
287
|
fields: senseRaw.fields,
|
|
285
288
|
},
|
|
@@ -59,6 +59,9 @@ export class SenseEngine {
|
|
|
59
59
|
const entry = {
|
|
60
60
|
spec,
|
|
61
61
|
cooldown: { until: 0 },
|
|
62
|
+
cooldownMs: spec.sense.cooldownMs,
|
|
63
|
+
minEvents: spec.sense.filter?.minEvents ?? 1,
|
|
64
|
+
eventCount: 0,
|
|
62
65
|
evaluator,
|
|
63
66
|
disposables: [],
|
|
64
67
|
};
|
|
@@ -92,6 +95,14 @@ export class SenseEngine {
|
|
|
92
95
|
entry.scheduleTimer.unref?.();
|
|
93
96
|
}
|
|
94
97
|
}
|
|
98
|
+
// 启动首跳:注册完成后补一次 tick(长 interval 首跳跨不过进程生命周期时的保底脉搏;
|
|
99
|
+
// setTimeout(0) 保证 builder 同步注册/依赖注入全部完成后才跑)
|
|
100
|
+
if (spec.sense.runOnStart) {
|
|
101
|
+
const starter = setTimeout(() => {
|
|
102
|
+
void this.onScheduleTick(spec.id, onTrigger);
|
|
103
|
+
}, 0);
|
|
104
|
+
starter.unref?.();
|
|
105
|
+
}
|
|
95
106
|
}
|
|
96
107
|
}
|
|
97
108
|
/**
|
|
@@ -283,6 +294,11 @@ export class SenseEngine {
|
|
|
283
294
|
const entry = this.subsystems.get(subsystemId);
|
|
284
295
|
if (!entry)
|
|
285
296
|
return;
|
|
297
|
+
// 累积计数先于冷却检查:冷却期只是不触发,增量照样攒(攒够 minEvents 才放行)
|
|
298
|
+
entry.eventCount += 1;
|
|
299
|
+
if (entry.eventCount < entry.minEvents) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
286
302
|
const now = Date.now();
|
|
287
303
|
if (now < entry.cooldown.until || entry.inFlight) {
|
|
288
304
|
return;
|
|
@@ -312,7 +328,8 @@ export class SenseEngine {
|
|
|
312
328
|
}
|
|
313
329
|
}
|
|
314
330
|
this.depthTracker.depth++;
|
|
315
|
-
entry.cooldown.until = Date.now() + this.defaultCooldownMs;
|
|
331
|
+
entry.cooldown.until = Date.now() + (entry.cooldownMs ?? this.defaultCooldownMs);
|
|
332
|
+
entry.eventCount = 0;
|
|
316
333
|
try {
|
|
317
334
|
onTrigger(ctx);
|
|
318
335
|
}
|
|
@@ -350,7 +367,7 @@ export class SenseEngine {
|
|
|
350
367
|
}
|
|
351
368
|
}
|
|
352
369
|
this.depthTracker.depth++;
|
|
353
|
-
entry.cooldown.until = Date.now() + this.defaultCooldownMs;
|
|
370
|
+
entry.cooldown.until = Date.now() + (entry.cooldownMs ?? this.defaultCooldownMs);
|
|
354
371
|
try {
|
|
355
372
|
onTrigger(ctx);
|
|
356
373
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 输出完整性保障 —— maxTokens 只是发给 provider 的生成配额,不是业务截断边界。
|
|
3
|
+
*
|
|
4
|
+
* 语义(2026-10-30 拍板):`finish=length`(配额截断)或 parse 失败的输出都**不得**当业务
|
|
5
|
+
* 结果收下(禁止静默半截结果);先放大配额重试一次,仍失败则返回显式失败原因,由调用方
|
|
6
|
+
* 决定如何上报。业务完整性优先于调用成本。
|
|
7
|
+
*
|
|
8
|
+
* @module harness/collaboration/autonomous-subsystem/think/completion-guard
|
|
9
|
+
*/
|
|
10
|
+
import type { LLMMessage } from '@octopi-agent/core/interfaces/model-provider.js';
|
|
11
|
+
import type { SubsystemLLMPort } from './llm-port.js';
|
|
12
|
+
/** 完整性调用的失败原因 */
|
|
13
|
+
export type CompletionGuardFailure = 'llm_error' | 'llm_empty' | 'truncated_output' | 'parse_failed';
|
|
14
|
+
/** 完整性调用结果:成功带解析后的业务结构,失败带显式原因 */
|
|
15
|
+
export type CompletionGuardResult<T> = {
|
|
16
|
+
ok: true;
|
|
17
|
+
parsed: T;
|
|
18
|
+
raw: string;
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
reason: CompletionGuardFailure;
|
|
22
|
+
message: string;
|
|
23
|
+
};
|
|
24
|
+
export interface CompletionGuardRequest {
|
|
25
|
+
/** 覆盖认知 prompt;默认使用 port 绑定的 SUBSYSTEM.md 正文 */
|
|
26
|
+
systemPrompt?: string;
|
|
27
|
+
/** 对话消息(不含 system) */
|
|
28
|
+
messages: LLMMessage[];
|
|
29
|
+
/** 生成配额(LLM 传参);截断时重试放大 2 倍,硬上限 {@link RETRY_MAX_TOKENS_CAP} */
|
|
30
|
+
maxTokens: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 带输出完整性保障的单轮 LLM 调用:截断或解析失败时放大配额重试一次。
|
|
34
|
+
*
|
|
35
|
+
* @param llmPort - subsystem LLM 端口
|
|
36
|
+
* @param req - systemPrompt / messages / maxTokens(生成配额,非业务截断)
|
|
37
|
+
* @param parse - 从 LLM 文本解析业务结构;失败返回 null
|
|
38
|
+
* @returns 成功返回 parsed;失败返回 llm_error / truncated_output / parse_failed(至多 2 次 LLM 调用)
|
|
39
|
+
*/
|
|
40
|
+
export declare function chatForCompleteOutput<T>(llmPort: SubsystemLLMPort, req: CompletionGuardRequest, parse: (text: string) => T | null): Promise<CompletionGuardResult<T>>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 输出完整性保障 —— maxTokens 只是发给 provider 的生成配额,不是业务截断边界。
|
|
3
|
+
*
|
|
4
|
+
* 语义(2026-10-30 拍板):`finish=length`(配额截断)或 parse 失败的输出都**不得**当业务
|
|
5
|
+
* 结果收下(禁止静默半截结果);先放大配额重试一次,仍失败则返回显式失败原因,由调用方
|
|
6
|
+
* 决定如何上报。业务完整性优先于调用成本。
|
|
7
|
+
*
|
|
8
|
+
* @module harness/collaboration/autonomous-subsystem/think/completion-guard
|
|
9
|
+
*/
|
|
10
|
+
/** 重试放大配额的硬上限(防 provider 400;超过此值仍截断按显式失败处理) */
|
|
11
|
+
const RETRY_MAX_TOKENS_CAP = 32768;
|
|
12
|
+
/**
|
|
13
|
+
* 带输出完整性保障的单轮 LLM 调用:截断或解析失败时放大配额重试一次。
|
|
14
|
+
*
|
|
15
|
+
* @param llmPort - subsystem LLM 端口
|
|
16
|
+
* @param req - systemPrompt / messages / maxTokens(生成配额,非业务截断)
|
|
17
|
+
* @param parse - 从 LLM 文本解析业务结构;失败返回 null
|
|
18
|
+
* @returns 成功返回 parsed;失败返回 llm_error / truncated_output / parse_failed(至多 2 次 LLM 调用)
|
|
19
|
+
*/
|
|
20
|
+
export async function chatForCompleteOutput(llmPort, req, parse) {
|
|
21
|
+
let lastFailure = 'parse_failed';
|
|
22
|
+
let lastMessage = 'parse_failed';
|
|
23
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
24
|
+
const maxTokens = attempt === 0 ? req.maxTokens : Math.min(req.maxTokens * 2, RETRY_MAX_TOKENS_CAP);
|
|
25
|
+
let res;
|
|
26
|
+
try {
|
|
27
|
+
res = await llmPort.chat({
|
|
28
|
+
systemPrompt: req.systemPrompt,
|
|
29
|
+
messages: req.messages,
|
|
30
|
+
// 温度留空:由模型配置(ModelInfo.temperature)或服务端默认决定
|
|
31
|
+
maxTokens,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
return { ok: false, reason: 'llm_error', message: err instanceof Error ? err.message : String(err) };
|
|
36
|
+
}
|
|
37
|
+
if (res.finishReason === 'error') {
|
|
38
|
+
return { ok: false, reason: 'llm_error', message: res.content || 'llm chat error' };
|
|
39
|
+
}
|
|
40
|
+
// 配额截断:半截输出不是业务结果,放大配额重试
|
|
41
|
+
if (res.finishReason === 'length') {
|
|
42
|
+
lastFailure = 'truncated_output';
|
|
43
|
+
lastMessage = `truncated_output: completion hit maxTokens=${maxTokens}`;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const raw = res.content ?? '';
|
|
47
|
+
if (!raw.trim()) {
|
|
48
|
+
lastFailure = 'llm_empty';
|
|
49
|
+
lastMessage = 'llm_empty: empty completion content';
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const parsed = parse(raw);
|
|
53
|
+
if (parsed !== null)
|
|
54
|
+
return { ok: true, parsed, raw };
|
|
55
|
+
lastFailure = 'parse_failed';
|
|
56
|
+
lastMessage = 'parse_failed';
|
|
57
|
+
}
|
|
58
|
+
return { ok: false, reason: lastFailure, message: lastMessage };
|
|
59
|
+
}
|
|
@@ -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,
|
|
@@ -4,3 +4,5 @@ export { ModelResolver, resolveProviderInstance } from './model-resolver.js';
|
|
|
4
4
|
export type { ModelResolverConfig, ResolvedModel, ResolvedModelWithFallback } from './model-resolver.js';
|
|
5
5
|
export { createSubsystemLLMPort, shouldFallbackModel, DEP_LLM_PORT, DEP_SUBSYSTEM_PROMPT, DEP_RESOLVED_MODEL, DEP_RESOLVED_MODELS, } from './llm-port.js';
|
|
6
6
|
export type { SubsystemLLMPort, SubsystemLLMPortChatRequest, CreateSubsystemLLMPortOptions, } from './llm-port.js';
|
|
7
|
+
export { chatForCompleteOutput } from './completion-guard.js';
|
|
8
|
+
export type { CompletionGuardFailure, CompletionGuardRequest, CompletionGuardResult, } from './completion-guard.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { ThinkExecutor } from './executor.js';
|
|
2
2
|
export { ModelResolver, resolveProviderInstance } from './model-resolver.js';
|
|
3
3
|
export { createSubsystemLLMPort, shouldFallbackModel, DEP_LLM_PORT, DEP_SUBSYSTEM_PROMPT, DEP_RESOLVED_MODEL, DEP_RESOLVED_MODELS, } from './llm-port.js';
|
|
4
|
+
export { chatForCompleteOutput } from './completion-guard.js';
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*
|
|
13
13
|
* @module autonomous-subsystem/think/model-resolver
|
|
14
14
|
*/
|
|
15
|
-
/** 产品约定的级别名(init/example/docs
|
|
15
|
+
/** 产品约定的级别名(init/example/docs 均定义 mini/standard/pro;summary 为保留别名——历史上曾是摘要档,保留只为把误写的档位名 fail-loud,而不是当模型名发给 provider) */
|
|
16
16
|
const WELLKNOWN_LEVEL_ALIASES = new Set(['summary', 'mini', 'standard', 'pro']);
|
|
17
17
|
/**
|
|
18
18
|
* 按解析结果选择 provider 实例
|