@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
|
@@ -123,9 +123,14 @@ export class HitlDecisionCache {
|
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
125
|
* Resolve through cache + host hook with in-flight de-dup.
|
|
126
|
-
* Concurrent callers for the same key share one `ask()
|
|
126
|
+
* Concurrent callers for the same key share one `ask()` and one outcome object
|
|
127
|
+
* (decision + timedOut + attribution — 合流者不得各自编造拒绝来源).
|
|
127
128
|
*/
|
|
128
129
|
resolve(options) {
|
|
130
|
+
return this.resolveOutcome(options).then((outcome) => outcome.allowed);
|
|
131
|
+
}
|
|
132
|
+
/** 同 {@link resolve},但回传完整结果(timedOut / 拒绝来源 / 归因),供错误面与审计口径一致。 */
|
|
133
|
+
resolveOutcome(options) {
|
|
129
134
|
const inflightKey = options.sessionId
|
|
130
135
|
? sessionKey(options.channel, options.sessionId, options.key)
|
|
131
136
|
: options.agentId
|
|
@@ -134,37 +139,48 @@ export class HitlDecisionCache {
|
|
|
134
139
|
const existing = this.inflight.get(inflightKey);
|
|
135
140
|
if (existing)
|
|
136
141
|
return existing;
|
|
137
|
-
const pending = this.
|
|
142
|
+
const pending = this.resolveOutcomeUncached(options).finally(() => {
|
|
138
143
|
this.inflight.delete(inflightKey);
|
|
139
144
|
});
|
|
140
145
|
this.inflight.set(inflightKey, pending);
|
|
141
146
|
return pending;
|
|
142
147
|
}
|
|
143
|
-
async
|
|
148
|
+
async resolveOutcomeUncached(options) {
|
|
144
149
|
const cached = this.get(options.channel, options.key, {
|
|
145
150
|
sessionId: options.sessionId,
|
|
146
151
|
agentId: options.agentId,
|
|
147
152
|
});
|
|
148
|
-
if (cached === 'approve')
|
|
149
|
-
return true;
|
|
150
|
-
|
|
153
|
+
if (cached === 'approve') {
|
|
154
|
+
return { allowed: true, decision: 'approve', timedOut: false };
|
|
155
|
+
}
|
|
156
|
+
let outcome;
|
|
151
157
|
try {
|
|
152
|
-
|
|
158
|
+
const raw = await options.ask();
|
|
159
|
+
const result = typeof raw === 'string' ? { decision: raw } : raw;
|
|
160
|
+
outcome = {
|
|
161
|
+
allowed: result.decision === 'approve' ||
|
|
162
|
+
result.decision === 'approve_session' ||
|
|
163
|
+
result.decision === 'approve_always',
|
|
164
|
+
decision: result.decision,
|
|
165
|
+
timedOut: result.timedOut ?? false,
|
|
166
|
+
...(result.denyReason !== undefined ? { denyReason: result.denyReason } : {}),
|
|
167
|
+
...(result.denyDetail !== undefined ? { denyDetail: result.denyDetail } : {}),
|
|
168
|
+
...(result.approver !== undefined ? { approver: result.approver } : {}),
|
|
169
|
+
...(result.channel !== undefined ? { channel: result.channel } : {}),
|
|
170
|
+
};
|
|
153
171
|
}
|
|
154
172
|
catch {
|
|
155
|
-
// 审批通道异常按拒绝处理(fail-safe
|
|
156
|
-
|
|
173
|
+
// 审批通道异常按拒绝处理(fail-safe);吞掉异常体只因「不放行」结论不变——
|
|
174
|
+
// 但来源如实标注 channel_error,异常绝不伪装成人工拒绝
|
|
175
|
+
outcome = { allowed: false, decision: 'deny', timedOut: false, denyReason: 'channel_error' };
|
|
157
176
|
}
|
|
158
|
-
if (
|
|
159
|
-
decision
|
|
160
|
-
decision === 'approve_always') {
|
|
161
|
-
this.remember(decision, options.channel, options.key, {
|
|
177
|
+
if (outcome.allowed) {
|
|
178
|
+
this.remember(outcome.decision, options.channel, options.key, {
|
|
162
179
|
sessionId: options.sessionId,
|
|
163
180
|
agentId: options.agentId,
|
|
164
181
|
});
|
|
165
|
-
return true;
|
|
166
182
|
}
|
|
167
|
-
return
|
|
183
|
+
return outcome;
|
|
168
184
|
}
|
|
169
185
|
}
|
|
170
186
|
/** Process-wide default cache (keys include sessionId / agentId). */
|
|
@@ -179,6 +195,16 @@ export const defaultHitlCache = new HitlDecisionCache();
|
|
|
179
195
|
* @returns true only when the call is approved (cached or live)
|
|
180
196
|
*/
|
|
181
197
|
export async function resolveHitlDecision(options) {
|
|
198
|
+
return (await resolveHitlOutcome(options)).allowed;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 同 {@link resolveHitlDecision},但回传完整结果(timedOut / 拒绝来源 / 归因)——
|
|
202
|
+
* 供调用方把错误面标注得与审计一致:超时 ≠ 人工拒绝 ≠ 通道故障,三者不得混为一谈。
|
|
203
|
+
*
|
|
204
|
+
* @param options - 同 resolveHitlDecision
|
|
205
|
+
* @returns 完整决策结果(同 key 并发合流共享同一对象)
|
|
206
|
+
*/
|
|
207
|
+
export async function resolveHitlOutcome(options) {
|
|
182
208
|
const cache = options.cache ?? defaultHitlCache;
|
|
183
|
-
return cache.
|
|
209
|
+
return cache.resolveOutcome(options);
|
|
184
210
|
}
|
|
@@ -423,6 +423,15 @@ export class DefaultSecurityGuard {
|
|
|
423
423
|
});
|
|
424
424
|
return { isClean: hardViolations.length === 0, violations: hardViolations, riskUnknown: true };
|
|
425
425
|
}
|
|
426
|
+
// 强不确定(解析结构性不确定):正交通知安全智能体过目,
|
|
427
|
+
// 等级判定与拦截行为照常(不因不确定放行,也不因不确定加拦)
|
|
428
|
+
if (decision.uncertain) {
|
|
429
|
+
this.emitRunEvent({
|
|
430
|
+
type: 'tool_call.risk_unknown',
|
|
431
|
+
timestamp: Date.now(),
|
|
432
|
+
data: { toolCall: call, decision },
|
|
433
|
+
});
|
|
434
|
+
}
|
|
426
435
|
const riskViolations = [];
|
|
427
436
|
if (decision.level !== 'low') {
|
|
428
437
|
const severityMap = {
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* SecurityGuard 接口定义在 Core 层。
|
|
6
6
|
*/
|
|
7
7
|
export { DefaultSecurityGuard } from './default-security-guard.js';
|
|
8
|
+
export { initShellParser, isShellParserReady, getShellParserEngine, } from './shell-parser.js';
|
|
8
9
|
export { isValidSecurityGuard, severityToAction } from '@octopi-agent/core/security-guard.js';
|
|
9
10
|
export type { SecurityGuard, SecurityCheckResult, SecurityViolation, SecurityViolationType, SecurityAction, BehaviorContext, SecurityGuardConfig, } from '@octopi-agent/core/interfaces/security-guard.js';
|
|
10
11
|
export type { ToolCallRiskPolicy } from '@octopi-agent/core/interfaces/security-guard.js';
|
|
@@ -6,5 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
// ── DefaultSecurityGuard 实现(canonical 位置) ──
|
|
8
8
|
export { DefaultSecurityGuard } from './default-security-guard.js';
|
|
9
|
+
// ── Shell 解析引擎(自装配方需自行 await initShellParser) ──
|
|
10
|
+
export { initShellParser, isShellParserReady, getShellParserEngine, } from './shell-parser.js';
|
|
9
11
|
// ── Core 接口 re-export(方便 harness 消费者) ──
|
|
10
12
|
export { isValidSecurityGuard, severityToAction } from '@octopi-agent/core/security-guard.js';
|
|
@@ -255,6 +255,7 @@ export function evaluateShellCommand(command, cwd) {
|
|
|
255
255
|
level: 'unknown',
|
|
256
256
|
factors: [{ source: 'method', description: '命令解析失败', level: 'unknown' }],
|
|
257
257
|
reason: '命令结构过于复杂,无法确定性解析',
|
|
258
|
+
uncertain: true,
|
|
258
259
|
};
|
|
259
260
|
}
|
|
260
261
|
// 1. 检测特殊模式(管道到解释器、内联代码、子 shell)
|
|
@@ -306,12 +307,24 @@ export function evaluateShellCommand(command, cwd) {
|
|
|
306
307
|
});
|
|
307
308
|
}
|
|
308
309
|
}
|
|
310
|
+
// 4.5 解析不确定(tree-sitter 报错回退 fallback 引擎):
|
|
311
|
+
// 结构可能被低估,显式计入 unknown 因子——有确定性高风险仍照常抬升,
|
|
312
|
+
// 全低风险时整体落 unknown 通道(放行 + 事件),不把不确定静默当安全。
|
|
313
|
+
if (parsed.uncertain) {
|
|
314
|
+
factors.push({
|
|
315
|
+
source: 'method',
|
|
316
|
+
description: '命令结构不确定(存在解析器无法确定性解析的片段)',
|
|
317
|
+
level: 'unknown',
|
|
318
|
+
});
|
|
319
|
+
}
|
|
309
320
|
// 5. 计算整体风险等级
|
|
310
321
|
const level = computeOverallRisk(factors);
|
|
311
322
|
return {
|
|
312
323
|
level,
|
|
313
324
|
factors,
|
|
314
325
|
reason: buildReason(level, factors),
|
|
326
|
+
// 强不确定与等级正交:无论等级高低都标记,供守卫上报安全智能体
|
|
327
|
+
...(parsed.uncertain ? { uncertain: true } : {}),
|
|
315
328
|
};
|
|
316
329
|
}
|
|
317
330
|
// ── 段评估 ──
|
|
@@ -42,6 +42,13 @@ export interface ParsedCommand {
|
|
|
42
42
|
hasSubshell: boolean;
|
|
43
43
|
/** 是否包含后台执行(&) */
|
|
44
44
|
hasBackground: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 解析不确定:tree-sitter 无法确定性解析(语法树含 ERROR)后回退 fallback 引擎。
|
|
47
|
+
* 风险分档应附加「结构不确定」因子,不允许静默当正常结果。
|
|
48
|
+
*/
|
|
49
|
+
uncertain?: boolean;
|
|
50
|
+
/** 未挂载到命令段的重定向(如 `> file` 单独成句):目标仍参与路径风险评估 */
|
|
51
|
+
orphanRedirects?: Redirect[];
|
|
45
52
|
}
|
|
46
53
|
export type RiskLevel = 'low' | 'medium' | 'high' | 'critical' | 'unknown';
|
|
47
54
|
export interface RiskFactor {
|
|
@@ -69,6 +76,11 @@ export interface RiskDecision {
|
|
|
69
76
|
alternative?: SafeAlternative;
|
|
70
77
|
/** 决策理由(人类可读) */
|
|
71
78
|
reason: string;
|
|
79
|
+
/**
|
|
80
|
+
* 强不确定(解析结构性不确定等):需安全智能体过目。
|
|
81
|
+
* 与等级正交——始终触发 risk_unknown 事件上报,但不改变等级判定与拦截行为。
|
|
82
|
+
*/
|
|
83
|
+
uncertain?: boolean;
|
|
72
84
|
}
|
|
73
85
|
export type PathRisk = 'safe' | 'normal' | 'sensitive' | 'protected';
|
|
74
86
|
export type ToolCategory = 'shell' | 'file' | 'http' | 'eval' | 'other';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell Parser (fallback) — 轻量级 Shell 命令解析器(回退引擎)
|
|
3
|
+
*
|
|
4
|
+
* 纯字符串解析。主引擎是 tree-sitter 适配层(shell-parser-treesitter.ts);
|
|
5
|
+
* 本解析器在 tree-sitter 不可用或解析报错时回退使用。
|
|
6
|
+
* 入口统一走 shell-parser.ts 的 parseShellCommand。
|
|
7
|
+
*
|
|
8
|
+
* 四层解析:
|
|
9
|
+
* 1. 按 |, &&, ||, ; 拆分 → 命令段
|
|
10
|
+
* 2. 每个命令段 → [wrapper...] + [command, ...args]
|
|
11
|
+
* 3. 识别 >, >>, < 重定向 → 提取目标路径
|
|
12
|
+
* 4. 识别 sudo, env, exec 等 wrapper → 解包实际命令
|
|
13
|
+
*
|
|
14
|
+
* 设计原则:宁可解析不完整,不可解析错误。
|
|
15
|
+
* 遇到无法解析的复杂结构,标记为 parsed: false,交给 LLM 判断。
|
|
16
|
+
*/
|
|
17
|
+
import type { ParsedCommand } from './risk-types.js';
|
|
18
|
+
/**
|
|
19
|
+
* 解析 Shell 命令(fallback 引擎)
|
|
20
|
+
*
|
|
21
|
+
* @param raw - 原始命令字符串
|
|
22
|
+
* @returns 解析结果(parsed: true/false 表示解析是否成功)
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseShellCommandFallback(raw: string): ParsedCommand & {
|
|
25
|
+
parsed: boolean;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 检测内联代码执行(共享实现,见 shell-parser-shared.ts)
|
|
29
|
+
*/
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell Parser (fallback) — 轻量级 Shell 命令解析器(回退引擎)
|
|
3
|
+
*
|
|
4
|
+
* 纯字符串解析。主引擎是 tree-sitter 适配层(shell-parser-treesitter.ts);
|
|
5
|
+
* 本解析器在 tree-sitter 不可用或解析报错时回退使用。
|
|
6
|
+
* 入口统一走 shell-parser.ts 的 parseShellCommand。
|
|
7
|
+
*
|
|
8
|
+
* 四层解析:
|
|
9
|
+
* 1. 按 |, &&, ||, ; 拆分 → 命令段
|
|
10
|
+
* 2. 每个命令段 → [wrapper...] + [command, ...args]
|
|
11
|
+
* 3. 识别 >, >>, < 重定向 → 提取目标路径
|
|
12
|
+
* 4. 识别 sudo, env, exec 等 wrapper → 解包实际命令
|
|
13
|
+
*
|
|
14
|
+
* 设计原则:宁可解析不完整,不可解析错误。
|
|
15
|
+
* 遇到无法解析的复杂结构,标记为 parsed: false,交给 LLM 判断。
|
|
16
|
+
*/
|
|
17
|
+
import { INTERPRETER_NAMES, BASH_ESCAPE_TARGETS, mergeSplitWindowsPaths, unwrapWrapperTokens, detectBackground, detectInlineCode, } from './shell-parser-shared.js';
|
|
18
|
+
// ── 解析入口 ──
|
|
19
|
+
/**
|
|
20
|
+
* 解析 Shell 命令(fallback 引擎)
|
|
21
|
+
*
|
|
22
|
+
* @param raw - 原始命令字符串
|
|
23
|
+
* @returns 解析结果(parsed: true/false 表示解析是否成功)
|
|
24
|
+
*/
|
|
25
|
+
export function parseShellCommandFallback(raw) {
|
|
26
|
+
const trimmed = raw.trim();
|
|
27
|
+
if (!trimmed) {
|
|
28
|
+
return {
|
|
29
|
+
parsed: true,
|
|
30
|
+
raw,
|
|
31
|
+
segments: [],
|
|
32
|
+
connectors: [],
|
|
33
|
+
hasShellPipe: false,
|
|
34
|
+
hasInlineCode: false,
|
|
35
|
+
hasSubshell: false,
|
|
36
|
+
hasBackground: false,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
// 检测子 shell 和后台执行
|
|
40
|
+
const hasSubshell = detectSubshell(trimmed);
|
|
41
|
+
const hasBackground = detectBackground(trimmed);
|
|
42
|
+
// 第一层:按连接符拆分
|
|
43
|
+
const { parts, connectors } = splitByConnectors(trimmed);
|
|
44
|
+
// 第二、三层:解析每个命令段
|
|
45
|
+
const segments = [];
|
|
46
|
+
let parsed = true;
|
|
47
|
+
for (const part of parts) {
|
|
48
|
+
const seg = parseSegment(part);
|
|
49
|
+
if (!seg) {
|
|
50
|
+
parsed = false;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
segments.push(seg);
|
|
54
|
+
}
|
|
55
|
+
// 第四层:检测特殊模式
|
|
56
|
+
const hasShellPipe = detectPipeToInterpreter(segments, connectors);
|
|
57
|
+
const hasInlineCode = detectInlineCode(segments);
|
|
58
|
+
return {
|
|
59
|
+
parsed,
|
|
60
|
+
raw,
|
|
61
|
+
segments,
|
|
62
|
+
connectors,
|
|
63
|
+
hasShellPipe,
|
|
64
|
+
hasInlineCode,
|
|
65
|
+
hasSubshell,
|
|
66
|
+
hasBackground,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// ── 第一层:按连接符拆分 ──
|
|
70
|
+
/**
|
|
71
|
+
* 按连接符拆分命令
|
|
72
|
+
*
|
|
73
|
+
* 注意处理引号内的连接符。
|
|
74
|
+
*/
|
|
75
|
+
function splitByConnectors(cmd) {
|
|
76
|
+
const parts = [];
|
|
77
|
+
const connectors = [];
|
|
78
|
+
let current = '';
|
|
79
|
+
let inSingleQuote = false;
|
|
80
|
+
let inDoubleQuote = false;
|
|
81
|
+
let escape = false;
|
|
82
|
+
let i = 0;
|
|
83
|
+
while (i < cmd.length) {
|
|
84
|
+
const ch = cmd[i];
|
|
85
|
+
// 转义字符
|
|
86
|
+
if (escape) {
|
|
87
|
+
current += ch;
|
|
88
|
+
escape = false;
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (ch === '\\') {
|
|
93
|
+
escape = true;
|
|
94
|
+
current += ch;
|
|
95
|
+
i++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// 引号状态
|
|
99
|
+
if (ch === "'" && !inDoubleQuote) {
|
|
100
|
+
inSingleQuote = !inSingleQuote;
|
|
101
|
+
current += ch;
|
|
102
|
+
i++;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (ch === '"' && !inSingleQuote) {
|
|
106
|
+
inDoubleQuote = !inDoubleQuote;
|
|
107
|
+
current += ch;
|
|
108
|
+
i++;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
// 在引号内,直接追加
|
|
112
|
+
if (inSingleQuote || inDoubleQuote) {
|
|
113
|
+
current += ch;
|
|
114
|
+
i++;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
// 检测连接符
|
|
118
|
+
if (ch === '|') {
|
|
119
|
+
if (cmd[i + 1] === '|') {
|
|
120
|
+
// ||
|
|
121
|
+
pushPart(parts, current);
|
|
122
|
+
current = '';
|
|
123
|
+
connectors.push('||');
|
|
124
|
+
i += 2;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
// |
|
|
129
|
+
pushPart(parts, current);
|
|
130
|
+
current = '';
|
|
131
|
+
connectors.push('|');
|
|
132
|
+
i++;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (ch === '&') {
|
|
137
|
+
if (cmd[i + 1] === '&') {
|
|
138
|
+
// &&
|
|
139
|
+
pushPart(parts, current);
|
|
140
|
+
current = '';
|
|
141
|
+
connectors.push('&&');
|
|
142
|
+
i += 2;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// `>&` / `N>&M`:文件描述符重定向(2>&1、>&2),不是命令分隔
|
|
146
|
+
if (i > 0 && cmd[i - 1] === '>') {
|
|
147
|
+
current += ch;
|
|
148
|
+
i++;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
// `&>` / `&>>`:bash 重定向合一,不是命令分隔
|
|
152
|
+
if (cmd[i + 1] === '>') {
|
|
153
|
+
current += ch;
|
|
154
|
+
i++;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
// 单 `&` 既是后台标记也是命令分隔:`true & rm -rf /` 必须拆段
|
|
158
|
+
pushPart(parts, current);
|
|
159
|
+
current = '';
|
|
160
|
+
connectors.push('&');
|
|
161
|
+
i++;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (ch === ';') {
|
|
165
|
+
pushPart(parts, current);
|
|
166
|
+
current = '';
|
|
167
|
+
connectors.push(';');
|
|
168
|
+
i++;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (ch === '\n') {
|
|
172
|
+
pushPart(parts, current);
|
|
173
|
+
current = '';
|
|
174
|
+
connectors.push('\n');
|
|
175
|
+
i++;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
current += ch;
|
|
179
|
+
i++;
|
|
180
|
+
}
|
|
181
|
+
// 最后一段
|
|
182
|
+
pushPart(parts, current);
|
|
183
|
+
return { parts, connectors };
|
|
184
|
+
}
|
|
185
|
+
function pushPart(parts, current) {
|
|
186
|
+
const trimmed = current.trim();
|
|
187
|
+
if (trimmed) {
|
|
188
|
+
parts.push(trimmed);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// ── 第二、三层:解析命令段 ──
|
|
192
|
+
/**
|
|
193
|
+
* 解析单个命令段
|
|
194
|
+
*
|
|
195
|
+
* 提取 wrapper、命令、参数、重定向。
|
|
196
|
+
* 遇到无法解析的结构返回 null。
|
|
197
|
+
*/
|
|
198
|
+
function parseSegment(raw) {
|
|
199
|
+
const rawTokens = tokenize(raw);
|
|
200
|
+
if (rawTokens.length === 0)
|
|
201
|
+
return null;
|
|
202
|
+
// 提取重定向(在 Windows 路径合并之前:目标语义按原始 token 粒度判定)
|
|
203
|
+
const redirects = [];
|
|
204
|
+
const cleanTokens = [];
|
|
205
|
+
let i = 0;
|
|
206
|
+
while (i < rawTokens.length) {
|
|
207
|
+
const token = rawTokens[i];
|
|
208
|
+
// 独立重定向操作符(`> file`):操作符与目标都必须是未引号字面量
|
|
209
|
+
if (isUnquotedToken(token) &&
|
|
210
|
+
(token.text === '>' || token.text === '>>' || token.text === '<')) {
|
|
211
|
+
const target = rawTokens[i + 1];
|
|
212
|
+
if (!target || !isUnquotedToken(target))
|
|
213
|
+
return null; // 重定向后没有目标,解析失败
|
|
214
|
+
redirects.push({
|
|
215
|
+
type: token.text === '>' ? 'overwrite' : token.text === '>>' ? 'append' : 'input',
|
|
216
|
+
target: target.text,
|
|
217
|
+
});
|
|
218
|
+
i += 2;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
// 检测 >file 格式(无空格)
|
|
222
|
+
const redirectMatch = matchRedirect(token);
|
|
223
|
+
if (redirectMatch) {
|
|
224
|
+
redirects.push(redirectMatch);
|
|
225
|
+
i++;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
cleanTokens.push(token.text);
|
|
229
|
+
i++;
|
|
230
|
+
}
|
|
231
|
+
if (cleanTokens.length === 0)
|
|
232
|
+
return null;
|
|
233
|
+
// 提取 wrapper(共享拆壳逻辑:sudo/env/exec 等)
|
|
234
|
+
const unwrapped = unwrapWrapperTokens(mergeSplitWindowsPaths(cleanTokens));
|
|
235
|
+
if (!unwrapped)
|
|
236
|
+
return null;
|
|
237
|
+
return {
|
|
238
|
+
command: unwrapped.command,
|
|
239
|
+
args: unwrapped.args,
|
|
240
|
+
redirects,
|
|
241
|
+
isSudo: unwrapped.isSudo,
|
|
242
|
+
wrappers: unwrapped.wrappers,
|
|
243
|
+
raw,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/** token 是否整体未引号 */
|
|
247
|
+
function isUnquotedToken(token) {
|
|
248
|
+
return token.quoted.every((q) => !q);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* 简单的 tokenizer
|
|
252
|
+
*
|
|
253
|
+
* 处理引号和转义字符,逐字符记录引号来源。
|
|
254
|
+
* Windows 路径(`C:\Windows`、`\\server\share`)中的 `\` 不会被吃掉——
|
|
255
|
+
* 否则保护路径判定会拿到被拆坏的字符串。
|
|
256
|
+
*/
|
|
257
|
+
function tokenize(raw) {
|
|
258
|
+
const tokens = [];
|
|
259
|
+
let current = '';
|
|
260
|
+
let mask = [];
|
|
261
|
+
let inSingleQuote = false;
|
|
262
|
+
let inDoubleQuote = false;
|
|
263
|
+
const pushChar = (ch, fromQuote) => {
|
|
264
|
+
current += ch;
|
|
265
|
+
mask.push(fromQuote);
|
|
266
|
+
};
|
|
267
|
+
const flush = () => {
|
|
268
|
+
if (current) {
|
|
269
|
+
tokens.push({ text: current, quoted: mask });
|
|
270
|
+
current = '';
|
|
271
|
+
mask = [];
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
for (let i = 0; i < raw.length; i++) {
|
|
275
|
+
const ch = raw[i];
|
|
276
|
+
const inQuote = inSingleQuote || inDoubleQuote;
|
|
277
|
+
if (ch === '\\' && !inSingleQuote) {
|
|
278
|
+
const next = raw[i + 1];
|
|
279
|
+
if (next === undefined) {
|
|
280
|
+
pushChar(ch, inQuote);
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
// 经典 bash 转义:吃掉 `\`,保留下一字符字面量
|
|
284
|
+
if (BASH_ESCAPE_TARGETS.has(next)) {
|
|
285
|
+
pushChar(next, inQuote);
|
|
286
|
+
i++;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
// Windows 路径分隔符 / UNC:保留 `\`
|
|
290
|
+
pushChar(ch, inQuote);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (ch === "'" && !inDoubleQuote) {
|
|
294
|
+
inSingleQuote = !inSingleQuote;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (ch === '"' && !inSingleQuote) {
|
|
298
|
+
inDoubleQuote = !inDoubleQuote;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if ((ch === ' ' || ch === '\t') && !inQuote) {
|
|
302
|
+
flush();
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
pushChar(ch, inQuote);
|
|
306
|
+
}
|
|
307
|
+
flush();
|
|
308
|
+
return tokens;
|
|
309
|
+
}
|
|
310
|
+
// ── 重定向识别(matchRedirect,含引号来源判定) ──
|
|
311
|
+
/**
|
|
312
|
+
* 匹配 >file 或 >>file 格式(无空格的重定向)
|
|
313
|
+
*
|
|
314
|
+
* 同时识别文件描述符重定向(`2>&1`、`>&2`、`&>file`)。
|
|
315
|
+
* 只在未引号区域识别:引号内的 `<`/`>` 是字面量(bash 语义)——
|
|
316
|
+
* 引号串被压成单 token 后,中缀启发式曾把引号内 `2>/dev/null` 误认成重定向、
|
|
317
|
+
* 其后整段捏造成目标(2026-10 假 critical 误拦的根因),按引号来源精确排除。
|
|
318
|
+
*
|
|
319
|
+
* @param token - token 文本与逐字符引号来源
|
|
320
|
+
* @returns 重定向信息;操作符或目标来自引号内时返回 null(宁可解析不完整,不可解析错误)
|
|
321
|
+
*/
|
|
322
|
+
function matchRedirect(token) {
|
|
323
|
+
const { text, quoted } = token;
|
|
324
|
+
/** [from, to) 是否全部未引号 */
|
|
325
|
+
const opAt = (from, to) => {
|
|
326
|
+
for (let k = from; k < to; k++) {
|
|
327
|
+
if (quoted[k])
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
return true;
|
|
331
|
+
};
|
|
332
|
+
/** 构造重定向;目标区域含引号内容则放弃(目标可能被引号串拼接污染) */
|
|
333
|
+
const make = (type, targetFrom) => {
|
|
334
|
+
if (!opAt(targetFrom, text.length))
|
|
335
|
+
return null;
|
|
336
|
+
return { type, target: text.slice(targetFrom) };
|
|
337
|
+
};
|
|
338
|
+
// `&>` / `&>>`:bash 重定向合一
|
|
339
|
+
if (text.startsWith('&>>') && opAt(0, 3)) {
|
|
340
|
+
return make('append', 3);
|
|
341
|
+
}
|
|
342
|
+
if (text.startsWith('&>') && text.length > 2 && opAt(0, 2)) {
|
|
343
|
+
return make('overwrite', 2);
|
|
344
|
+
}
|
|
345
|
+
// 文件描述符重定向:`2>&1`、`>&2`、`1>&2` 等
|
|
346
|
+
// target 用 fd: 标记,避免被当成路径做风险分类;整个 token 必须未引号
|
|
347
|
+
const fdDup = text.match(/^(\d*)>{1,2}&(\d+)$/);
|
|
348
|
+
if (fdDup && opAt(0, text.length)) {
|
|
349
|
+
return {
|
|
350
|
+
type: text.includes('>>') ? 'append' : 'overwrite',
|
|
351
|
+
target: `fd:${fdDup[2]}`,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
// 先检查开头的重定向
|
|
355
|
+
if (text.startsWith('>>') && opAt(0, 2)) {
|
|
356
|
+
return make('append', 2);
|
|
357
|
+
}
|
|
358
|
+
if (text.startsWith('>') && text.length > 1 && opAt(0, 1)) {
|
|
359
|
+
return make('overwrite', 1);
|
|
360
|
+
}
|
|
361
|
+
if (text.startsWith('<') && text.length > 1 && opAt(0, 1)) {
|
|
362
|
+
return make('input', 1);
|
|
363
|
+
}
|
|
364
|
+
// 检查 token 中间的重定向(如 hello>/tmp/out.txt);`>&N` 已由 fdDup 覆盖
|
|
365
|
+
const gtIdx = text.indexOf('>');
|
|
366
|
+
if (gtIdx > 0 && gtIdx < text.length - 1 && opAt(gtIdx, gtIdx + 1)) {
|
|
367
|
+
if (text[gtIdx + 1] === '&') {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
if (text[gtIdx + 1] === '>') {
|
|
371
|
+
return make('append', gtIdx + 2);
|
|
372
|
+
}
|
|
373
|
+
return make('overwrite', gtIdx + 1);
|
|
374
|
+
}
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
// ── 第四层:检测特殊模式 ──
|
|
378
|
+
/**
|
|
379
|
+
* 检测子 shell($(...) 或反引号)
|
|
380
|
+
*/
|
|
381
|
+
function detectSubshell(cmd) {
|
|
382
|
+
// $(...) 模式
|
|
383
|
+
if (cmd.includes('$('))
|
|
384
|
+
return true;
|
|
385
|
+
// 反引号模式
|
|
386
|
+
if (cmd.includes('`'))
|
|
387
|
+
return true;
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* 检测管道到解释器
|
|
392
|
+
*
|
|
393
|
+
* 不止 sh/bash,python/ruby/perl/node 等也是解释器。
|
|
394
|
+
* curl ... | python3 和 curl ... | sh 本质相同(执行外部代码)。
|
|
395
|
+
*/
|
|
396
|
+
function detectPipeToInterpreter(segments, connectors) {
|
|
397
|
+
for (let i = 0; i < connectors.length; i++) {
|
|
398
|
+
if (connectors[i] === '|') {
|
|
399
|
+
const nextSeg = segments[i + 1];
|
|
400
|
+
if (nextSeg && INTERPRETER_NAMES.has(nextSeg.command) && nextSeg.args.length === 0) {
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* 检测内联代码执行(共享实现,见 shell-parser-shared.ts)
|
|
409
|
+
*/
|
|
410
|
+
// ── 导出工具函数移至 shell-parser.ts facade ──
|