@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
|
@@ -51,6 +51,7 @@ export async function runAllowlistedArgv(options) {
|
|
|
51
51
|
allowRoots: [cwd, ...journalRoots],
|
|
52
52
|
networkPolicy: context?.networkPolicy ?? 'deny',
|
|
53
53
|
requireJail: context?.requireJail,
|
|
54
|
+
disableJail: context?.disableJail,
|
|
54
55
|
homeWrite: context?.homeWrite,
|
|
55
56
|
noteSecurityDegraded: context?.noteSecurityDegraded,
|
|
56
57
|
signal: context?.abortSignal,
|
|
@@ -59,6 +60,38 @@ export async function runAllowlistedArgv(options) {
|
|
|
59
60
|
maxOutputBytes: 16 * 1024 * 1024,
|
|
60
61
|
},
|
|
61
62
|
});
|
|
63
|
+
// ops P2 §7:统一动作记录(allowlisted exec 同契约;P3 提权通道复用此形态)
|
|
64
|
+
try {
|
|
65
|
+
const { isJailBackend } = await import('../../execution-environment/process-isolation.js');
|
|
66
|
+
context?.recordOpsEvent?.({
|
|
67
|
+
event: 'ops.command.executed',
|
|
68
|
+
data: {
|
|
69
|
+
tool: 'allowlisted_run',
|
|
70
|
+
argv: [executable, ...args],
|
|
71
|
+
cwd,
|
|
72
|
+
exitCode: sandboxResult.exitCode,
|
|
73
|
+
durationMs: sandboxResult.usage.elapsedMs,
|
|
74
|
+
escalated: false,
|
|
75
|
+
// 子步执行的确认态由所属工具的效应闸门承担,本层无从得知——记 null(未知),
|
|
76
|
+
// 不用 false/true 冒充
|
|
77
|
+
confirmed: null,
|
|
78
|
+
confirmSource: 'not-gated-here',
|
|
79
|
+
jail: {
|
|
80
|
+
backend: sandboxResult.isolation.backend,
|
|
81
|
+
unconfined: context?.disableJail === true || !isJailBackend(sandboxResult.isolation.backend),
|
|
82
|
+
},
|
|
83
|
+
originAttributed: Boolean(context?.sessionId),
|
|
84
|
+
origin: {
|
|
85
|
+
sessionId: context?.sessionId,
|
|
86
|
+
agentId: context?.agentId,
|
|
87
|
+
runId: context?.runId ?? null,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// 审计回传失败不阻断业务结果(与下方 journal 同策略)
|
|
94
|
+
}
|
|
62
95
|
// Journal after-diff (same contract as shell tool §5.6)
|
|
63
96
|
try {
|
|
64
97
|
const sj = await import('../../execution-environment/shell-journal.js');
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* 通过工厂函数接收 AskUserCallback(闭包注入),与 UI 层解耦。
|
|
5
5
|
* 回调持有「等回答」的 Promise;Gateway/UI 提供答案后工具才返回。
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* 无超时(2026-10-26 拍板):需要用户确认的问题不能被时间自动跳过。
|
|
8
|
+
* 退出只有两路:会话中止(abortSignal)或 Gateway 取消(ASK_USER_CANCELLED 哨兵)。
|
|
9
|
+
* definition 不声明 timeoutMs,工具总线(runWithToolLimits)因此不设看门狗。
|
|
8
10
|
*/
|
|
9
11
|
import type { RegisteredTool } from '@octopi-agent/core/types.js';
|
|
10
12
|
/** askUser 回调上下文(绑定到具体会话) */
|
|
@@ -27,8 +29,5 @@ export declare const ASK_USER_CANCELLED = "__ask_user_cancelled__";
|
|
|
27
29
|
* 创建 ask_user 工具
|
|
28
30
|
*
|
|
29
31
|
* @param callback - 等待用户回答的回调(通常由 Gateway 挂 pending question)
|
|
30
|
-
* @param options.timeoutMs - 等待上限;超时返回错误而不是悬挂
|
|
31
32
|
*/
|
|
32
|
-
export declare function createAskUserTool(callback: AskUserCallback
|
|
33
|
-
timeoutMs?: number;
|
|
34
|
-
}): RegisteredTool;
|
|
33
|
+
export declare function createAskUserTool(callback: AskUserCallback): RegisteredTool;
|
|
@@ -4,25 +4,23 @@
|
|
|
4
4
|
* 通过工厂函数接收 AskUserCallback(闭包注入),与 UI 层解耦。
|
|
5
5
|
* 回调持有「等回答」的 Promise;Gateway/UI 提供答案后工具才返回。
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* 无超时(2026-10-26 拍板):需要用户确认的问题不能被时间自动跳过。
|
|
8
|
+
* 退出只有两路:会话中止(abortSignal)或 Gateway 取消(ASK_USER_CANCELLED 哨兵)。
|
|
9
|
+
* definition 不声明 timeoutMs,工具总线(runWithToolLimits)因此不设看门狗。
|
|
8
10
|
*/
|
|
9
11
|
/** Gateway cancel 哨兵(与 gateway.ASK_USER_CANCELLED 同值,避免 harness→integration 依赖) */
|
|
10
12
|
export const ASK_USER_CANCELLED = '__ask_user_cancelled__';
|
|
11
|
-
/** 默认等待用户回答的上限(与 definition.timeoutMs 对齐) */
|
|
12
|
-
const DEFAULT_ASK_TIMEOUT_MS = 300_000;
|
|
13
13
|
/**
|
|
14
14
|
* 创建 ask_user 工具
|
|
15
15
|
*
|
|
16
16
|
* @param callback - 等待用户回答的回调(通常由 Gateway 挂 pending question)
|
|
17
|
-
* @param options.timeoutMs - 等待上限;超时返回错误而不是悬挂
|
|
18
17
|
*/
|
|
19
|
-
export function createAskUserTool(callback
|
|
20
|
-
const timeoutMs = options?.timeoutMs ?? DEFAULT_ASK_TIMEOUT_MS;
|
|
18
|
+
export function createAskUserTool(callback) {
|
|
21
19
|
return {
|
|
22
20
|
definition: {
|
|
23
21
|
name: 'ask_user',
|
|
24
22
|
capabilities: [],
|
|
25
|
-
description: 'Ask the user a question and wait for their response. Use when you need clarification, confirmation, or a choice from the user. Optionally provide a list of suggested options. Do not use for things you can look up yourself.',
|
|
23
|
+
description: 'Ask the user a question and wait for their response. Use when you need clarification, confirmation, or a choice from the user. Optionally provide a list of suggested options. Do not use for things you can look up yourself. There is no timeout — the question waits until the user answers, or the run is aborted.',
|
|
26
24
|
parameters: {
|
|
27
25
|
question: {
|
|
28
26
|
type: 'string',
|
|
@@ -35,7 +33,6 @@ export function createAskUserTool(callback, options) {
|
|
|
35
33
|
items: { type: 'string', description: 'An option' },
|
|
36
34
|
},
|
|
37
35
|
},
|
|
38
|
-
timeoutMs,
|
|
39
36
|
},
|
|
40
37
|
handler: async (args, context) => {
|
|
41
38
|
const question = String(args.question ?? '').trim();
|
|
@@ -54,11 +51,8 @@ export function createAskUserTool(callback, options) {
|
|
|
54
51
|
return { answer: null, error: 'aborted before asking user' };
|
|
55
52
|
}
|
|
56
53
|
const abortSignal = context?.abortSignal;
|
|
57
|
-
let timer;
|
|
58
54
|
const answer = await new Promise((resolve, reject) => {
|
|
59
55
|
const cleanup = () => {
|
|
60
|
-
if (timer !== undefined)
|
|
61
|
-
clearTimeout(timer);
|
|
62
56
|
abortSignal?.removeEventListener('abort', onAbort);
|
|
63
57
|
};
|
|
64
58
|
const onAbort = () => {
|
|
@@ -72,10 +66,6 @@ export function createAskUserTool(callback, options) {
|
|
|
72
66
|
return;
|
|
73
67
|
}
|
|
74
68
|
}
|
|
75
|
-
timer = setTimeout(() => {
|
|
76
|
-
cleanup();
|
|
77
|
-
reject(new Error(`ask_user timed out after ${timeoutMs}ms`));
|
|
78
|
-
}, timeoutMs);
|
|
79
69
|
callback(question, options, ctx).then((value) => {
|
|
80
70
|
cleanup();
|
|
81
71
|
resolve(value);
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* - file_search: 跨文件内容搜索
|
|
9
9
|
* - http_request: HTTP 请求
|
|
10
10
|
* - env_info: 运行环境信息
|
|
11
|
+
* - process_list: 只读进程诊断(进程表/资源/祖先链)
|
|
12
|
+
* - priv_exec: 白名单提权执行(ops P3,全等匹配 + 确认闸)
|
|
11
13
|
*
|
|
12
14
|
* 所有有状态工具(memory、task、ask_user)通过 extension-tools 按需注入。
|
|
13
15
|
*
|
|
@@ -32,6 +34,7 @@ import type { RegisteredTool } from '@octopi-agent/core/types.js';
|
|
|
32
34
|
* - exitCode: 退出码
|
|
33
35
|
* - durationMs: 执行耗时
|
|
34
36
|
* - shell: 实际使用的 shell(kind/executable)
|
|
37
|
+
* - hints: 沙箱归因提示(仅当 jail 拒绝并已归因时出现,如 setuid exec)
|
|
35
38
|
*/
|
|
36
39
|
export declare function createShellTool(options?: {
|
|
37
40
|
summary?: import('../../../context/capabilities/summary/index.js').ToolSummarySupport;
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* - file_search: 跨文件内容搜索
|
|
9
9
|
* - http_request: HTTP 请求
|
|
10
10
|
* - env_info: 运行环境信息
|
|
11
|
+
* - process_list: 只读进程诊断(进程表/资源/祖先链)
|
|
12
|
+
* - priv_exec: 白名单提权执行(ops P3,全等匹配 + 确认闸)
|
|
11
13
|
*
|
|
12
14
|
* 所有有状态工具(memory、task、ask_user)通过 extension-tools 按需注入。
|
|
13
15
|
*
|
|
@@ -26,8 +28,12 @@ import { createGitTools } from './git-tools.js';
|
|
|
26
28
|
import { createPackageTools } from './package-tools.js';
|
|
27
29
|
import { createHttpRequestTool } from './http.js';
|
|
28
30
|
import { createEnvInfoTool } from './env-info.js';
|
|
31
|
+
import { createProcessListTool } from './process-tools.js';
|
|
32
|
+
import { createPrivExecTool } from './privileged-tool.js';
|
|
29
33
|
import { createDocumentProbeTool, createDocumentReadTool } from './document-tools.js';
|
|
30
|
-
import {
|
|
34
|
+
import { containmentOptions, defaultPathEnv, isPathUnderRoots, isUnderAttachmentRoot, isUnderSessionUndoRoot, resolvePlatformShell, resolveReadableToolPath, resolveToolPath, } from './platform.js';
|
|
35
|
+
import { readFileNoFollow } from '../../../shared/paths/index.js';
|
|
36
|
+
import { matchesDenyPattern } from '../../../shared/paths/glob.js';
|
|
31
37
|
/**
|
|
32
38
|
* Shell 工具 — 执行 shell 命令
|
|
33
39
|
*
|
|
@@ -42,6 +48,7 @@ import { isUnderSessionUndoRoot, defaultPathEnv, isPathUnderRoots, isUnderAttach
|
|
|
42
48
|
* - exitCode: 退出码
|
|
43
49
|
* - durationMs: 执行耗时
|
|
44
50
|
* - shell: 实际使用的 shell(kind/executable)
|
|
51
|
+
* - hints: 沙箱归因提示(仅当 jail 拒绝并已归因时出现,如 setuid exec)
|
|
45
52
|
*/
|
|
46
53
|
export function createShellTool(options) {
|
|
47
54
|
const shell = resolvePlatformShell();
|
|
@@ -54,7 +61,7 @@ export function createShellTool(options) {
|
|
|
54
61
|
definition: {
|
|
55
62
|
name: 'shell',
|
|
56
63
|
capabilities: ['exec'],
|
|
57
|
-
description: `LAST RESORT shell. 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/git_status/git_diff/git_log/git_show/git_commit/git_branch/git_restore/package_edit/project_run/package_install and any task-specific tools). Use shell only when: no dedicated tool covers the operation, the dedicated tool is unavailable, or it still fails after a correct retry (wrong path/args → fix and retry the tool first). Never use shell instead of a working dedicated tool. Detected shell: ${shell.label}. ${syntaxHint}`,
|
|
64
|
+
description: `LAST RESORT shell. 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/git_status/git_diff/git_log/git_show/git_commit/git_branch/git_restore/package_edit/project_run/package_install and any task-specific tools). Use shell only when: no dedicated tool covers the operation, the dedicated tool is unavailable, or it still fails after a correct retry (wrong path/args → fix and retry the tool first). Never use shell instead of a working dedicated tool. Detected shell: ${shell.label}. ${syntaxHint}`,
|
|
58
65
|
parameters: {
|
|
59
66
|
command: {
|
|
60
67
|
type: 'string',
|
|
@@ -76,7 +83,7 @@ export function createShellTool(options) {
|
|
|
76
83
|
handler: async (args, context) => {
|
|
77
84
|
const command = args.command;
|
|
78
85
|
const cwd = args.cwd ?? context?.cwd ?? process.cwd();
|
|
79
|
-
await gateEffect({ kind: 'shell', command: args.command, cwd: typeof cwd === 'string' ? cwd : undefined }, {
|
|
86
|
+
const gateResult = await gateEffect({ kind: 'shell', command: args.command, cwd: typeof cwd === 'string' ? cwd : undefined }, {
|
|
80
87
|
effects: context?.effectChannel ?? 'engine',
|
|
81
88
|
confirmEffect: context?.confirmEffect,
|
|
82
89
|
shellEscape: context?.shellEscape,
|
|
@@ -107,10 +114,7 @@ export function createShellTool(options) {
|
|
|
107
114
|
notReversedPayload = { reason: 'shell_journal_off' };
|
|
108
115
|
}
|
|
109
116
|
const resolvedShell = resolvePlatformShell();
|
|
110
|
-
const resolvedCwd = resolveToolPath(cwd, context?.cwd,
|
|
111
|
-
allowRoots: context?.pathRoots,
|
|
112
|
-
containment: context?.pathContainment,
|
|
113
|
-
});
|
|
117
|
+
const resolvedCwd = resolveToolPath(cwd, context?.cwd, containmentOptions(context));
|
|
114
118
|
// D2-a: always ProcessSandbox (jail backend when probed, else supervised).
|
|
115
119
|
const { createProcessSandbox } = await import('../../execution-environment/sandbox.js');
|
|
116
120
|
const sandbox = await createProcessSandbox({
|
|
@@ -122,6 +126,7 @@ export function createShellTool(options) {
|
|
|
122
126
|
allowRoots: [resolvedCwd, ...(context?.pathRoots ?? []).filter(Boolean)],
|
|
123
127
|
networkPolicy: context?.networkPolicy ?? 'deny',
|
|
124
128
|
requireJail: context?.requireJail,
|
|
129
|
+
disableJail: context?.disableJail,
|
|
125
130
|
homeWrite: context?.homeWrite,
|
|
126
131
|
noteSecurityDegraded: context?.noteSecurityDegraded,
|
|
127
132
|
signal: context?.abortSignal,
|
|
@@ -130,6 +135,43 @@ export function createShellTool(options) {
|
|
|
130
135
|
maxOutputBytes: 16 * 1024 * 1024,
|
|
131
136
|
},
|
|
132
137
|
});
|
|
138
|
+
// ops P2 §7:统一动作记录(argv/cwd/退出码/耗时/确认态/jail)——审计失败不阻断业务
|
|
139
|
+
try {
|
|
140
|
+
const { isJailBackend } = await import('../../execution-environment/process-isolation.js');
|
|
141
|
+
context?.recordOpsEvent?.({
|
|
142
|
+
event: 'ops.command.executed',
|
|
143
|
+
data: {
|
|
144
|
+
tool: 'shell',
|
|
145
|
+
argv: [resolvedShell.executable, ...resolvedShell.args, command],
|
|
146
|
+
cwd: resolvedCwd,
|
|
147
|
+
exitCode: sandboxResult.exitCode,
|
|
148
|
+
durationMs: sandboxResult.usage.elapsedMs,
|
|
149
|
+
escalated: false,
|
|
150
|
+
// 确认态取闸门实际放行结论(通道配置≠本次决策;自动放行如实记 false)
|
|
151
|
+
confirmed: gateResult.confirmed,
|
|
152
|
+
confirmSource: gateResult.confirmSource,
|
|
153
|
+
// ops P5:确认人归因(人工批准时随闸门回传;缓存命中/自动放行如实缺省)
|
|
154
|
+
...(gateResult.confirmedBy !== undefined ? { confirmedBy: gateResult.confirmedBy } : {}),
|
|
155
|
+
...(gateResult.confirmChannel !== undefined
|
|
156
|
+
? { confirmChannel: gateResult.confirmChannel }
|
|
157
|
+
: {}),
|
|
158
|
+
jail: {
|
|
159
|
+
backend: sandboxResult.isolation.backend,
|
|
160
|
+
unconfined: context?.disableJail === true || !isJailBackend(sandboxResult.isolation.backend),
|
|
161
|
+
},
|
|
162
|
+
// 注入源不明标记(§5 P2):工具调用带 session/run 归属 = 已归因
|
|
163
|
+
originAttributed: Boolean(context?.sessionId),
|
|
164
|
+
origin: {
|
|
165
|
+
sessionId: context?.sessionId,
|
|
166
|
+
agentId: context?.agentId,
|
|
167
|
+
runId: context?.runId ?? null,
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
// 审计回传失败不阻断 shell 业务结果(写失败的降级申报在 runner 侧)
|
|
174
|
+
}
|
|
133
175
|
try {
|
|
134
176
|
// §5.6:after diff 入账本 / 不可逆清单
|
|
135
177
|
try {
|
|
@@ -182,6 +224,9 @@ export function createShellTool(options) {
|
|
|
182
224
|
terminated: sandboxResult.terminated,
|
|
183
225
|
terminateReason: sandboxResult.terminateReason,
|
|
184
226
|
isolation: sandboxResult.isolation,
|
|
227
|
+
...(sandboxResult.diagnostics?.hints?.length
|
|
228
|
+
? { hints: sandboxResult.diagnostics.hints }
|
|
229
|
+
: {}),
|
|
185
230
|
shell: {
|
|
186
231
|
kind: resolvedShell.kind,
|
|
187
232
|
executable: resolvedShell.executable,
|
|
@@ -200,6 +245,9 @@ export function createShellTool(options) {
|
|
|
200
245
|
terminated: sandboxResult.terminated,
|
|
201
246
|
terminateReason: sandboxResult.terminateReason,
|
|
202
247
|
isolation: sandboxResult.isolation,
|
|
248
|
+
...(sandboxResult.diagnostics?.hints?.length
|
|
249
|
+
? { hints: sandboxResult.diagnostics.hints }
|
|
250
|
+
: {}),
|
|
203
251
|
shell: {
|
|
204
252
|
kind: resolvedShell.kind,
|
|
205
253
|
executable: resolvedShell.executable,
|
|
@@ -275,14 +323,13 @@ export function createFileReadTool(options) {
|
|
|
275
323
|
const pathModule = await import('node:path');
|
|
276
324
|
const rawPath = args.path;
|
|
277
325
|
const cwd = context?.cwd ?? process.cwd();
|
|
278
|
-
const path = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots,
|
|
279
|
-
allowRoots: context?.pathRoots,
|
|
280
|
-
containment: context?.pathContainment,
|
|
281
|
-
});
|
|
326
|
+
const path = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots, containmentOptions(context));
|
|
282
327
|
const offset = args.offset ?? 1;
|
|
283
328
|
const limit = args.limit ?? 2000;
|
|
284
329
|
try {
|
|
285
|
-
const content =
|
|
330
|
+
const content = context?.refuseFinalSymlink
|
|
331
|
+
? await readFileNoFollow(path, 'utf8')
|
|
332
|
+
: await readFile(path, 'utf-8');
|
|
286
333
|
const lines = content.split('\n');
|
|
287
334
|
const totalLines = lines.length;
|
|
288
335
|
const start = Math.max(0, offset - 1);
|
|
@@ -362,10 +409,7 @@ export function createFileWriteTool() {
|
|
|
362
409
|
const { dirname } = await import('node:path');
|
|
363
410
|
const rawPath = args.path;
|
|
364
411
|
const cwd = context?.cwd ?? process.cwd();
|
|
365
|
-
const path = resolveToolPath(rawPath, cwd,
|
|
366
|
-
allowRoots: context?.pathRoots,
|
|
367
|
-
containment: context?.pathContainment,
|
|
368
|
-
});
|
|
412
|
+
const path = resolveToolPath(rawPath, cwd, containmentOptions(context));
|
|
369
413
|
if (isUnderAttachmentRoot(path, context?.attachmentRoots)) {
|
|
370
414
|
throw new Error('session attachments are read-only; cannot write to attachments root');
|
|
371
415
|
}
|
|
@@ -397,10 +441,10 @@ export function createFileWriteTool() {
|
|
|
397
441
|
throw new Error('file_effects_not_configured: file_write requires ToolExecutionContext.fileEffects');
|
|
398
442
|
}
|
|
399
443
|
if (append) {
|
|
400
|
-
await effects.appendFile(path, content);
|
|
444
|
+
await effects.appendFile(path, content, { refuseFinalSymlink: context?.refuseFinalSymlink === true });
|
|
401
445
|
}
|
|
402
446
|
else {
|
|
403
|
-
await effects.writeFile(path, content);
|
|
447
|
+
await effects.writeFile(path, content, { refuseFinalSymlink: context?.refuseFinalSymlink === true });
|
|
404
448
|
}
|
|
405
449
|
return {
|
|
406
450
|
path,
|
|
@@ -442,10 +486,7 @@ export function createFileDeleteTool() {
|
|
|
442
486
|
const { readFile, rm, stat } = await import('node:fs/promises');
|
|
443
487
|
const rawPath = args.path;
|
|
444
488
|
const cwd = context?.cwd ?? process.cwd();
|
|
445
|
-
const path = resolveToolPath(rawPath, cwd,
|
|
446
|
-
allowRoots: context?.pathRoots,
|
|
447
|
-
containment: context?.pathContainment,
|
|
448
|
-
});
|
|
489
|
+
const path = resolveToolPath(rawPath, cwd, containmentOptions(context));
|
|
449
490
|
if (isUnderAttachmentRoot(path, context?.attachmentRoots)) {
|
|
450
491
|
throw new Error('session attachments are read-only; cannot delete from attachments root');
|
|
451
492
|
}
|
|
@@ -596,10 +637,7 @@ export function createFileListTool(options) {
|
|
|
596
637
|
const { join, relative } = await import('node:path');
|
|
597
638
|
const rawPath = args.path ?? '.';
|
|
598
639
|
const cwd = context?.cwd ?? process.cwd();
|
|
599
|
-
const basePath = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots,
|
|
600
|
-
allowRoots: context?.pathRoots,
|
|
601
|
-
containment: context?.pathContainment,
|
|
602
|
-
});
|
|
640
|
+
const basePath = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots, containmentOptions(context));
|
|
603
641
|
// 找文件意图(带 pattern)默认递归;显式 recursive:false 仍只列本层
|
|
604
642
|
const hasPattern = Boolean(args.pattern?.trim());
|
|
605
643
|
const recursive = args.recursive === undefined && hasPattern
|
|
@@ -609,6 +647,7 @@ export function createFileListTool(options) {
|
|
|
609
647
|
const maxEntries = Math.min(Math.max(args.maxEntries ?? 500, 1), 2000);
|
|
610
648
|
const maxDepth = Math.min(Math.max(args.maxDepth ?? 4, 1), 8);
|
|
611
649
|
const entries = [];
|
|
650
|
+
const denylist = context?.pathDenylist ?? [];
|
|
612
651
|
let totalCount = 0;
|
|
613
652
|
let entryCapped = false;
|
|
614
653
|
let depthCapped = false;
|
|
@@ -625,6 +664,9 @@ export function createFileListTool(options) {
|
|
|
625
664
|
return;
|
|
626
665
|
const fullPath = join(dir, item.name);
|
|
627
666
|
const relativePath = relative(basePath, fullPath);
|
|
667
|
+
// Denied names are not even enumerated: existence and size are data too.
|
|
668
|
+
if (denylist.length > 0 && matchesDenyPattern(relativePath.split('\\').join('/'), denylist))
|
|
669
|
+
continue;
|
|
628
670
|
if (recursive && item.isDirectory() && SKIP_DIR_NAMES.has(item.name))
|
|
629
671
|
continue;
|
|
630
672
|
// pattern 只过滤列出的条目;递归时目录始终下钻(否则 recursive+*.md 进不了子目录)
|
|
@@ -771,6 +813,8 @@ export function getBuiltinTools(options) {
|
|
|
771
813
|
requireBindings: options?.requireCredentialBindings,
|
|
772
814
|
}),
|
|
773
815
|
createEnvInfoTool(),
|
|
816
|
+
createProcessListTool(),
|
|
817
|
+
createPrivExecTool(),
|
|
774
818
|
createShellTool({ summary: options?.summary }),
|
|
775
819
|
];
|
|
776
820
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 只消费 DocumentPort;业务/设计知识留在 skill。
|
|
5
5
|
*/
|
|
6
|
-
import { resolveReadableToolPath } from './platform.js';
|
|
6
|
+
import { containmentOptions, resolveReadableToolPath, } from './platform.js';
|
|
7
7
|
/**
|
|
8
8
|
* document_read — 抽取 PDF/Office 为 Markdown
|
|
9
9
|
*
|
|
@@ -44,10 +44,7 @@ export function createDocumentReadTool(options) {
|
|
|
44
44
|
}
|
|
45
45
|
const rawPath = String(args.path ?? '');
|
|
46
46
|
const cwd = context?.cwd ?? process.cwd();
|
|
47
|
-
const path = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots,
|
|
48
|
-
allowRoots: context?.pathRoots,
|
|
49
|
-
containment: context?.pathContainment,
|
|
50
|
-
});
|
|
47
|
+
const path = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots, containmentOptions(context));
|
|
51
48
|
try {
|
|
52
49
|
const result = await port.extract({
|
|
53
50
|
path,
|
|
@@ -135,10 +132,7 @@ export function createDocumentProbeTool(options) {
|
|
|
135
132
|
}
|
|
136
133
|
const rawPath = String(args.path ?? '');
|
|
137
134
|
const cwd = context?.cwd ?? process.cwd();
|
|
138
|
-
const path = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots,
|
|
139
|
-
allowRoots: context?.pathRoots,
|
|
140
|
-
containment: context?.pathContainment,
|
|
141
|
-
});
|
|
135
|
+
const path = resolveReadableToolPath(rawPath, cwd, context?.attachmentRoots, containmentOptions(context));
|
|
142
136
|
try {
|
|
143
137
|
const probe = await port.probe({ path, name: path });
|
|
144
138
|
const caps = await port.capabilities();
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* - dry-run 预览模式
|
|
11
11
|
* - 编辑后自动返回周围上下文
|
|
12
12
|
*/
|
|
13
|
-
import { isPathUnderRoots, isUnderAttachmentRoot, isUnderSessionUndoRoot, resolveToolPath, } from './platform.js';
|
|
13
|
+
import { containmentOptions, isPathUnderRoots, isUnderAttachmentRoot, isUnderSessionUndoRoot, resolveToolPath, } from './platform.js';
|
|
14
14
|
import { gateEffect } from '../../execution-environment/effect-policy.js';
|
|
15
15
|
import { detectEol, oldTextCandidates, toEol } from './eol.js';
|
|
16
16
|
export function createFileEditTool() {
|
|
@@ -51,10 +51,7 @@ export function createFileEditTool() {
|
|
|
51
51
|
const { readFile } = await import('node:fs/promises');
|
|
52
52
|
const rawPath = args.path;
|
|
53
53
|
const cwd = context.cwd ?? process.cwd();
|
|
54
|
-
const path = resolveToolPath(rawPath, cwd,
|
|
55
|
-
allowRoots: context.pathRoots,
|
|
56
|
-
containment: context.pathContainment,
|
|
57
|
-
});
|
|
54
|
+
const path = resolveToolPath(rawPath, cwd, containmentOptions(context));
|
|
58
55
|
if (isUnderAttachmentRoot(path, context.attachmentRoots)) {
|
|
59
56
|
throw new Error('session attachments are read-only; cannot edit attachments root');
|
|
60
57
|
}
|
|
@@ -136,7 +133,7 @@ export function createFileEditTool() {
|
|
|
136
133
|
if (!effects) {
|
|
137
134
|
throw new Error('file_effects_not_configured: file_edit requires ToolExecutionContext.fileEffects');
|
|
138
135
|
}
|
|
139
|
-
await effects.writeFile(path, newContent);
|
|
136
|
+
await effects.writeFile(path, newContent, { refuseFinalSymlink: context?.refuseFinalSymlink === true });
|
|
140
137
|
}
|
|
141
138
|
return {
|
|
142
139
|
path,
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { isPathUnderRoots, isUnderAttachmentRoot,
|
|
1
|
+
import { containmentOptions, isPathUnderRoots, isUnderAttachmentRoot, isUnderSessionUndoRoot, resolveToolPath, } from './platform.js';
|
|
2
2
|
import { gateEffect } from '../../execution-environment/effect-policy.js';
|
|
3
|
+
import { isFinalSymlink } from '../../../shared/paths/index.js';
|
|
3
4
|
function resolveSafePath(rawPath, context, label) {
|
|
4
5
|
const cwd = context?.cwd ?? process.cwd();
|
|
5
|
-
const path = resolveToolPath(rawPath, cwd,
|
|
6
|
-
allowRoots: context?.pathRoots,
|
|
7
|
-
containment: context?.pathContainment,
|
|
8
|
-
});
|
|
6
|
+
const path = resolveToolPath(rawPath, cwd, containmentOptions(context));
|
|
9
7
|
if (isUnderAttachmentRoot(path, context?.attachmentRoots)) {
|
|
10
8
|
throw new Error(`session attachments are read-only; cannot ${label} under attachments root`);
|
|
11
9
|
}
|
|
@@ -256,6 +254,12 @@ export function createFileCopyTool() {
|
|
|
256
254
|
await effects.recordCreate(to, after);
|
|
257
255
|
}
|
|
258
256
|
// Binary-safe copy (images, Office, etc.)
|
|
257
|
+
// `copyFile` opens the destination with O_TRUNC and would follow a symlink
|
|
258
|
+
// planted at `to`, writing outside the root. It also bypasses
|
|
259
|
+
// `effects.writeFile`, so the final-component guard must be applied here.
|
|
260
|
+
if (context?.refuseFinalSymlink === true && isFinalSymlink(to)) {
|
|
261
|
+
throw new Error(`path_forbidden: "${to}" final component is a symlink (refuseFinalSymlink)`);
|
|
262
|
+
}
|
|
259
263
|
await copyFile(from, to);
|
|
260
264
|
return {
|
|
261
265
|
from,
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
* - 零结果时返回 diagnostics + hints,便于调用方自纠
|
|
22
22
|
*/
|
|
23
23
|
import { resolveSupportBinding, } from '../../../context/capabilities/summary/index.js';
|
|
24
|
-
import { resolveToolPath } from './platform.js';
|
|
24
|
+
import { containmentOptions, resolveToolPath, } from './platform.js';
|
|
25
|
+
import { globToPathRegex, matchesDenyPattern } from '../../../shared/paths/glob.js';
|
|
25
26
|
/** 需要忽略的目录 */
|
|
26
27
|
const IGNORED_DIRS = new Set([
|
|
27
28
|
'node_modules', '.git', '.svn', '.hg', 'dist', 'build',
|
|
@@ -117,10 +118,7 @@ export function createFileSearchTool(options) {
|
|
|
117
118
|
const { basename, extname, join, relative } = await import('node:path');
|
|
118
119
|
const rawRoot = args.path;
|
|
119
120
|
const rootPath = rawRoot
|
|
120
|
-
? resolveToolPath(rawRoot, context.cwd ?? process.cwd(),
|
|
121
|
-
allowRoots: context.pathRoots,
|
|
122
|
-
containment: context.pathContainment,
|
|
123
|
-
})
|
|
121
|
+
? resolveToolPath(rawRoot, context.cwd ?? process.cwd(), containmentOptions(context))
|
|
124
122
|
: (context.cwd ?? process.cwd());
|
|
125
123
|
const terms = collectPatternTerms(args);
|
|
126
124
|
const patternMode = resolvePatternMode(args);
|
|
@@ -153,6 +151,12 @@ export function createFileSearchTool(options) {
|
|
|
153
151
|
async function considerFile(fullPath, relPath) {
|
|
154
152
|
if (truncated)
|
|
155
153
|
return;
|
|
154
|
+
// Denylist applies per file, not just to the `path` argument: this tool
|
|
155
|
+
// walks a tree and returns file *contents*, so a denied name would
|
|
156
|
+
// otherwise be readable here while `file_read` refuses it.
|
|
157
|
+
const denylist = context.pathDenylist ?? [];
|
|
158
|
+
if (denylist.length > 0 && matchesDenyPattern(relPath, denylist))
|
|
159
|
+
return;
|
|
156
160
|
filesSeen += 1;
|
|
157
161
|
// glob 过滤:root 相对路径(无 `/` 的模式等价于任意深度 globstar)
|
|
158
162
|
if (fileFilter && !fileFilter.test(relPath))
|
|
@@ -574,131 +578,6 @@ async function searchFileLines(options) {
|
|
|
574
578
|
}
|
|
575
579
|
}
|
|
576
580
|
// ── glob 编译 ──
|
|
577
|
-
/**
|
|
578
|
-
* 将路径 glob 编译为正则(匹配 root 相对路径)
|
|
579
|
-
*
|
|
580
|
-
* 语义对齐 rg/git:
|
|
581
|
-
* - `**` 跨目录;`*` / `?` 不跨 `/`
|
|
582
|
-
* - 无 `/` 的模式(如 `*.md`)等价于任意深度的 globstar 匹配
|
|
583
|
-
* - 支持 `{a,b}` 花括号(可多组)
|
|
584
|
-
*
|
|
585
|
-
* @param glob - glob 模式
|
|
586
|
-
* @returns 整串匹配相对路径的正则(大小写不敏感)
|
|
587
|
-
*/
|
|
588
|
-
function globToPathRegex(glob) {
|
|
589
|
-
const trimmed = glob.trim();
|
|
590
|
-
if (!trimmed)
|
|
591
|
-
throw new Error('glob must be non-empty');
|
|
592
|
-
const normalized = trimmed.includes('/') ? trimmed : `**/${trimmed}`;
|
|
593
|
-
const source = globToSource(normalized);
|
|
594
|
-
return new RegExp(`^${source}$`, 'i');
|
|
595
|
-
}
|
|
596
|
-
/**
|
|
597
|
-
* 将(已规范化)glob 片段编译为正则源
|
|
598
|
-
*
|
|
599
|
-
* @param glob - 含路径分隔符的 glob
|
|
600
|
-
* @returns 正则源片段(无首尾锚点)
|
|
601
|
-
*/
|
|
602
|
-
function globToSource(glob) {
|
|
603
|
-
let out = '';
|
|
604
|
-
let i = 0;
|
|
605
|
-
while (i < glob.length) {
|
|
606
|
-
const ch = glob[i];
|
|
607
|
-
// `\*` `\?` 等 → 字面量
|
|
608
|
-
if (ch === '\\' && i + 1 < glob.length && '*?[]{}\\/!'.includes(glob[i + 1])) {
|
|
609
|
-
out += escapeRegex(glob[i + 1]);
|
|
610
|
-
i += 2;
|
|
611
|
-
continue;
|
|
612
|
-
}
|
|
613
|
-
if (ch === '*' && glob[i + 1] === '*') {
|
|
614
|
-
i += 2;
|
|
615
|
-
if (glob[i] === '/') {
|
|
616
|
-
i += 1;
|
|
617
|
-
// `**/` → 任意深度前缀(含零层)
|
|
618
|
-
out += '(?:.*/)?';
|
|
619
|
-
}
|
|
620
|
-
else {
|
|
621
|
-
out += '.*';
|
|
622
|
-
}
|
|
623
|
-
continue;
|
|
624
|
-
}
|
|
625
|
-
if (ch === '*') {
|
|
626
|
-
i += 1;
|
|
627
|
-
out += '[^/]*';
|
|
628
|
-
continue;
|
|
629
|
-
}
|
|
630
|
-
if (ch === '?') {
|
|
631
|
-
i += 1;
|
|
632
|
-
out += '[^/]';
|
|
633
|
-
continue;
|
|
634
|
-
}
|
|
635
|
-
if (ch === '{') {
|
|
636
|
-
const end = findBraceEnd(glob, i);
|
|
637
|
-
if (end === -1) {
|
|
638
|
-
out += '\\{';
|
|
639
|
-
i += 1;
|
|
640
|
-
continue;
|
|
641
|
-
}
|
|
642
|
-
const alts = splitBraceAlts(glob.slice(i + 1, end));
|
|
643
|
-
out += `(?:${alts.map((alt) => globToSource(alt)).join('|')})`;
|
|
644
|
-
i = end + 1;
|
|
645
|
-
continue;
|
|
646
|
-
}
|
|
647
|
-
if ('.+^$()|[]'.includes(ch)) {
|
|
648
|
-
out += `\\${ch}`;
|
|
649
|
-
}
|
|
650
|
-
else {
|
|
651
|
-
out += ch;
|
|
652
|
-
}
|
|
653
|
-
i += 1;
|
|
654
|
-
}
|
|
655
|
-
return out;
|
|
656
|
-
}
|
|
657
|
-
/**
|
|
658
|
-
* 找到配对的 `}`(跳过嵌套 `{}`)
|
|
659
|
-
*
|
|
660
|
-
* @param glob - glob 字符串
|
|
661
|
-
* @param openIndex - `{` 下标
|
|
662
|
-
* @returns 配对 `}` 下标;无则 -1
|
|
663
|
-
*/
|
|
664
|
-
function findBraceEnd(glob, openIndex) {
|
|
665
|
-
let depth = 0;
|
|
666
|
-
for (let i = openIndex; i < glob.length; i++) {
|
|
667
|
-
if (glob[i] === '{')
|
|
668
|
-
depth += 1;
|
|
669
|
-
else if (glob[i] === '}') {
|
|
670
|
-
depth -= 1;
|
|
671
|
-
if (depth === 0)
|
|
672
|
-
return i;
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
return -1;
|
|
676
|
-
}
|
|
677
|
-
/**
|
|
678
|
-
* 拆分花括号内的顶层逗号备选
|
|
679
|
-
*
|
|
680
|
-
* @param body - `{}` 内部
|
|
681
|
-
* @returns 备选列表
|
|
682
|
-
*/
|
|
683
|
-
function splitBraceAlts(body) {
|
|
684
|
-
const alts = [];
|
|
685
|
-
let depth = 0;
|
|
686
|
-
let current = '';
|
|
687
|
-
for (const ch of body) {
|
|
688
|
-
if (ch === '{')
|
|
689
|
-
depth += 1;
|
|
690
|
-
if (ch === '}')
|
|
691
|
-
depth -= 1;
|
|
692
|
-
if (ch === ',' && depth === 0) {
|
|
693
|
-
alts.push(current);
|
|
694
|
-
current = '';
|
|
695
|
-
continue;
|
|
696
|
-
}
|
|
697
|
-
current += ch;
|
|
698
|
-
}
|
|
699
|
-
alts.push(current);
|
|
700
|
-
return alts;
|
|
701
|
-
}
|
|
702
581
|
// ── L1 输出收缩(F1-B1:结构化硬顶,零 LLM) ──
|
|
703
582
|
/**
|
|
704
583
|
* L1 结构化收缩:把 matches 清单体压进预算,定位符(file+line)永远全保
|