@octopi-agent/engine 0.97.15 → 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-schema/agent.d.ts +2 -0
- package/dist/config-schema/engine.d.ts +4 -0
- package/dist/config-schema/governance.d.ts +2 -0
- package/dist/config-schema/governance.js +30 -0
- package/dist/harness/agent/builder.d.ts +11 -0
- package/dist/harness/agent/builder.js +59 -32
- package/dist/harness/agent/config-integrity.d.ts +66 -0
- package/dist/harness/agent/config-integrity.js +89 -0
- package/dist/harness/agent/wire-agent-builder.js +5 -0
- 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/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/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/layers.d.ts +2 -4
- package/dist/harness/context/layers.js +9 -11
- package/dist/harness/context/summarize.d.ts +1 -1
- package/dist/harness/context/summarize.js +2 -2
- package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
- package/dist/harness/context/system-prompt-assembler.js +1 -0
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +24 -2
- package/dist/harness/extension/execution-environment/effect-policy.js +30 -0
- package/dist/harness/extension/execution-environment/ops-audit.d.ts +6 -2
- package/dist/harness/extension/execution-environment/ops-audit.js +11 -12
- 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/tools/builtin.js +17 -24
- 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/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/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/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/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/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/run/run-scope.d.ts +4 -0
- package/dist/harness/run/runner.d.ts +27 -2
- package/dist/harness/run/runner.js +48 -4
- 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/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 +7 -2
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell Parser (tree-sitter) — 主解析引擎
|
|
3
|
+
*
|
|
4
|
+
* 基于 web-tree-sitter + tree-sitter-bash(vendored wasm,见 vendor/)。
|
|
5
|
+
* 容错解析:引号、`$()`、heredoc、`[[ ]]`、重定向等结构由语法树精确还原,
|
|
6
|
+
* 不再依赖字符串启发式(字符串解析历史上曾在引号内 `2>/dev/null` 类文本上误判)。
|
|
7
|
+
*
|
|
8
|
+
* 适配职责:把 bash 语法树映射回 ParsedCommand/ParsedSegment 契约,
|
|
9
|
+
* 风险评估(risk-evaluator)与降级策略(degradation)无需感知引擎差异。
|
|
10
|
+
*
|
|
11
|
+
* 映射约定:
|
|
12
|
+
* - `variable_assignment`(`VAR=val`)/ `declaration_command`(export/declare/local)
|
|
13
|
+
* 是声明不是命令执行,不产生命令段;值里的 `$(...)` 仍按子 shell 检测。
|
|
14
|
+
* - 命令替换/进程替换/子 shell(`$(...)`、反引号、`<(…)`、`(…)`)里的命令
|
|
15
|
+
* 会真实执行,因此展开为独立命令段参与风险评估。
|
|
16
|
+
* - heredoc 正文是 stdin 数据:字面内容不解析、不产生路径目标;定界符未加引号时
|
|
17
|
+
* 正文里的 `$(...)` 属真实执行,照常展开为命令段。
|
|
18
|
+
* - 整句重定向(如 `for …; done > /dev/null`)挂在句内第一个命令段上;
|
|
19
|
+
* 句内无命令时(`> file` 单独成句)记入 orphanRedirects,仍参与路径评估。
|
|
20
|
+
* - 语法树含 ERROR/MISSING 时返回 null,由 facade 回退 fallback 引擎并标记不确定。
|
|
21
|
+
*
|
|
22
|
+
* 初始化是异步的(WASM 加载);解析是同步的。生产路径在 Agent 构建时
|
|
23
|
+
* await initShellParser(),未初始化时 facade 自动回退 fallback 引擎。
|
|
24
|
+
*/
|
|
25
|
+
import type { ParsedCommand } from './risk-types.js';
|
|
26
|
+
/**
|
|
27
|
+
* 初始化 tree-sitter 解析引擎(幂等)
|
|
28
|
+
*
|
|
29
|
+
* 失败不抛错:降级到 fallback 引擎(parseShellCommand 自动回退),仅告警。
|
|
30
|
+
*/
|
|
31
|
+
export declare function initShellParser(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* tree-sitter 引擎是否就绪
|
|
34
|
+
*
|
|
35
|
+
* @returns 是否已成功初始化
|
|
36
|
+
*/
|
|
37
|
+
export declare function isShellParserReady(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* 当前生效的解析引擎
|
|
40
|
+
*
|
|
41
|
+
* @returns 'treesitter' 或 'fallback'
|
|
42
|
+
*/
|
|
43
|
+
export declare function getShellParserEngine(): 'treesitter' | 'fallback';
|
|
44
|
+
/**
|
|
45
|
+
* 用 tree-sitter 解析 Shell 命令
|
|
46
|
+
*
|
|
47
|
+
* @param raw - 原始命令字符串
|
|
48
|
+
* @returns 解析结果;引擎未就绪、语法树含 ERROR 或内部异常时返回 null(调用方回退)
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseShellCommandWithTreeSitter(raw: string): (ParsedCommand & {
|
|
51
|
+
parsed: boolean;
|
|
52
|
+
}) | null;
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell Parser (tree-sitter) — 主解析引擎
|
|
3
|
+
*
|
|
4
|
+
* 基于 web-tree-sitter + tree-sitter-bash(vendored wasm,见 vendor/)。
|
|
5
|
+
* 容错解析:引号、`$()`、heredoc、`[[ ]]`、重定向等结构由语法树精确还原,
|
|
6
|
+
* 不再依赖字符串启发式(字符串解析历史上曾在引号内 `2>/dev/null` 类文本上误判)。
|
|
7
|
+
*
|
|
8
|
+
* 适配职责:把 bash 语法树映射回 ParsedCommand/ParsedSegment 契约,
|
|
9
|
+
* 风险评估(risk-evaluator)与降级策略(degradation)无需感知引擎差异。
|
|
10
|
+
*
|
|
11
|
+
* 映射约定:
|
|
12
|
+
* - `variable_assignment`(`VAR=val`)/ `declaration_command`(export/declare/local)
|
|
13
|
+
* 是声明不是命令执行,不产生命令段;值里的 `$(...)` 仍按子 shell 检测。
|
|
14
|
+
* - 命令替换/进程替换/子 shell(`$(...)`、反引号、`<(…)`、`(…)`)里的命令
|
|
15
|
+
* 会真实执行,因此展开为独立命令段参与风险评估。
|
|
16
|
+
* - heredoc 正文是 stdin 数据:字面内容不解析、不产生路径目标;定界符未加引号时
|
|
17
|
+
* 正文里的 `$(...)` 属真实执行,照常展开为命令段。
|
|
18
|
+
* - 整句重定向(如 `for …; done > /dev/null`)挂在句内第一个命令段上;
|
|
19
|
+
* 句内无命令时(`> file` 单独成句)记入 orphanRedirects,仍参与路径评估。
|
|
20
|
+
* - 语法树含 ERROR/MISSING 时返回 null,由 facade 回退 fallback 引擎并标记不确定。
|
|
21
|
+
*
|
|
22
|
+
* 初始化是异步的(WASM 加载);解析是同步的。生产路径在 Agent 构建时
|
|
23
|
+
* await initShellParser(),未初始化时 facade 自动回退 fallback 引擎。
|
|
24
|
+
*/
|
|
25
|
+
import { INTERPRETER_NAMES, unescapeToken, mergeSplitWindowsPaths, unwrapWrapperTokens, detectBackground, detectInlineCode, } from './shell-parser-shared.js';
|
|
26
|
+
// ── 初始化 ──
|
|
27
|
+
let parserInstance = null;
|
|
28
|
+
let initPromise = null;
|
|
29
|
+
/**
|
|
30
|
+
* 初始化 tree-sitter 解析引擎(幂等)
|
|
31
|
+
*
|
|
32
|
+
* 失败不抛错:降级到 fallback 引擎(parseShellCommand 自动回退),仅告警。
|
|
33
|
+
*/
|
|
34
|
+
export async function initShellParser() {
|
|
35
|
+
if (parserInstance)
|
|
36
|
+
return;
|
|
37
|
+
initPromise ??= doInit();
|
|
38
|
+
await initPromise;
|
|
39
|
+
}
|
|
40
|
+
async function doInit() {
|
|
41
|
+
try {
|
|
42
|
+
const { Parser: TsParser, Language: TsLanguage } = await import('web-tree-sitter');
|
|
43
|
+
await TsParser.init();
|
|
44
|
+
const language = await TsLanguage.load(new URL('./vendor/tree-sitter-bash.wasm', import.meta.url));
|
|
45
|
+
const instance = new TsParser();
|
|
46
|
+
instance.setLanguage(language);
|
|
47
|
+
parserInstance = instance;
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
// 吞掉的是 WASM 加载/编译失败(环境问题,重试不会改变结果):
|
|
51
|
+
// 解析能力降级到 fallback 引擎,功能不中断;此处是加载失败唯一会到达的路径。
|
|
52
|
+
console.warn('[shell-parser] tree-sitter 初始化失败,回退 fallback 解析器:', err);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* tree-sitter 引擎是否就绪
|
|
57
|
+
*
|
|
58
|
+
* @returns 是否已成功初始化
|
|
59
|
+
*/
|
|
60
|
+
export function isShellParserReady() {
|
|
61
|
+
return parserInstance !== null;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 当前生效的解析引擎
|
|
65
|
+
*
|
|
66
|
+
* @returns 'treesitter' 或 'fallback'
|
|
67
|
+
*/
|
|
68
|
+
export function getShellParserEngine() {
|
|
69
|
+
return parserInstance ? 'treesitter' : 'fallback';
|
|
70
|
+
}
|
|
71
|
+
// ── 解析入口 ──
|
|
72
|
+
/**
|
|
73
|
+
* 用 tree-sitter 解析 Shell 命令
|
|
74
|
+
*
|
|
75
|
+
* @param raw - 原始命令字符串
|
|
76
|
+
* @returns 解析结果;引擎未就绪、语法树含 ERROR 或内部异常时返回 null(调用方回退)
|
|
77
|
+
*/
|
|
78
|
+
export function parseShellCommandWithTreeSitter(raw) {
|
|
79
|
+
if (!parserInstance)
|
|
80
|
+
return null;
|
|
81
|
+
try {
|
|
82
|
+
const tree = parserInstance.parse(raw);
|
|
83
|
+
try {
|
|
84
|
+
if (!tree || tree.rootNode.hasError)
|
|
85
|
+
return null;
|
|
86
|
+
return mapTree(raw, tree.rootNode);
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
// 映射已把数据拷成普通对象;显式释放 WASM 侧树内存(热路径,不等 GC)
|
|
90
|
+
tree?.delete();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// 吞掉的是 web-tree-sitter 对极端输入的内部异常(如超长/异常字节序列):
|
|
95
|
+
// 返回 null 由 facade 回退 fallback 并标记不确定;解析本身无其他失败路径。
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function mapTree(raw, root) {
|
|
100
|
+
const ctx = { segments: [], orphans: [] };
|
|
101
|
+
extract(root, ctx, null);
|
|
102
|
+
const spanned = ctx.segments.sort((a, b) => a.start - b.start);
|
|
103
|
+
const segments = spanned.map((s) => s.seg);
|
|
104
|
+
const connectors = buildConnectors(raw, spanned);
|
|
105
|
+
return {
|
|
106
|
+
parsed: true,
|
|
107
|
+
raw,
|
|
108
|
+
segments,
|
|
109
|
+
connectors,
|
|
110
|
+
hasShellPipe: detectShellPipe(root),
|
|
111
|
+
hasInlineCode: detectInlineCode(segments),
|
|
112
|
+
hasSubshell: detectSubshellNodes(root),
|
|
113
|
+
hasBackground: detectBackground(raw),
|
|
114
|
+
orphanRedirects: ctx.orphans.length > 0 ? ctx.orphans : undefined,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 提取命令段
|
|
119
|
+
*
|
|
120
|
+
* @param node - 当前语法树节点
|
|
121
|
+
* @param ctx - 收集器
|
|
122
|
+
* @param spanNode - 整句包装节点(redirected_statement),用于连接符定位
|
|
123
|
+
*/
|
|
124
|
+
function extract(node, ctx, spanNode) {
|
|
125
|
+
switch (node.type) {
|
|
126
|
+
case 'redirected_statement': {
|
|
127
|
+
const redirects = [];
|
|
128
|
+
let body = null;
|
|
129
|
+
for (const child of node.namedChildren) {
|
|
130
|
+
if (child.type === 'file_redirect') {
|
|
131
|
+
const redirect = mapFileRedirect(child);
|
|
132
|
+
if (redirect)
|
|
133
|
+
redirects.push(redirect);
|
|
134
|
+
// 重定向目标里的命令替换仍会执行
|
|
135
|
+
extract(child, ctx, null);
|
|
136
|
+
}
|
|
137
|
+
else if (child.type === 'heredoc_redirect') {
|
|
138
|
+
// heredoc 正文是 stdin 数据:字面内容不执行,也不产生路径目标;
|
|
139
|
+
// 但定界符未加引号时正文里的 $(...) 会真实执行
|
|
140
|
+
// (语法树只在未引号 heredoc 中展开命令替换)——照常下钻提取
|
|
141
|
+
extract(child, ctx, null);
|
|
142
|
+
}
|
|
143
|
+
else if (!body) {
|
|
144
|
+
body = child;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const before = ctx.segments.length;
|
|
148
|
+
if (body)
|
|
149
|
+
extract(body, ctx, node);
|
|
150
|
+
if (redirects.length > 0) {
|
|
151
|
+
if (ctx.segments.length > before) {
|
|
152
|
+
// 整句重定向挂在句内第一个命令段上(目标风险由重定向目标评估兜底)
|
|
153
|
+
ctx.segments[before].seg.redirects.push(...redirects);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
// 句内无命令(`> file` 单独成句):目标仍可能是写入,不可丢
|
|
157
|
+
ctx.orphans.push(...redirects);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
case 'command': {
|
|
163
|
+
const seg = mapCommand(node);
|
|
164
|
+
if (seg) {
|
|
165
|
+
const span = spanNode ?? node;
|
|
166
|
+
ctx.segments.push({ seg, start: span.startIndex, end: span.endIndex });
|
|
167
|
+
}
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
default:
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
// 通用递归:命令替换/子 shell/进程替换里的命令会真实执行
|
|
174
|
+
for (const child of node.namedChildren) {
|
|
175
|
+
extract(child, ctx, null);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* 映射单条命令节点
|
|
180
|
+
*
|
|
181
|
+
* @param node - command 节点
|
|
182
|
+
* @returns 命令段;无命令名(纯赋值等)时返回 null
|
|
183
|
+
*/
|
|
184
|
+
function mapCommand(node) {
|
|
185
|
+
let commandName = null;
|
|
186
|
+
const argNodes = [];
|
|
187
|
+
const redirects = [];
|
|
188
|
+
for (const child of node.namedChildren) {
|
|
189
|
+
// 命令名之前的 KEY=VAL 是环境赋值不是参数;命令名之后的是实参
|
|
190
|
+
if (child.type === 'variable_assignment' && !commandName)
|
|
191
|
+
continue;
|
|
192
|
+
if (child.type === 'file_redirect') {
|
|
193
|
+
// 挂在命令节点自身的重定向(少见形态):目标不可静默丢弃
|
|
194
|
+
const redirect = mapFileRedirect(child);
|
|
195
|
+
if (redirect)
|
|
196
|
+
redirects.push(redirect);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (child.type === 'heredoc_redirect')
|
|
200
|
+
continue;
|
|
201
|
+
if (!commandName && child.type === 'command_name') {
|
|
202
|
+
commandName = child;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
argNodes.push(child);
|
|
206
|
+
}
|
|
207
|
+
if (!commandName)
|
|
208
|
+
return null;
|
|
209
|
+
const command = wordValue(commandName);
|
|
210
|
+
const args = argNodes.map(wordValue);
|
|
211
|
+
// Windows 未加引号的空格路径先合并(`del C:\Program Files\App\x.exe`),
|
|
212
|
+
// 再走与 fallback 相同的 wrapper 拆壳;两引擎此语义必须一致
|
|
213
|
+
const unwrapped = unwrapWrapperTokens(mergeSplitWindowsPaths([command, ...args]));
|
|
214
|
+
if (!unwrapped)
|
|
215
|
+
return null;
|
|
216
|
+
return {
|
|
217
|
+
command: unwrapped.command,
|
|
218
|
+
args: unwrapped.args,
|
|
219
|
+
redirects,
|
|
220
|
+
isSudo: unwrapped.isSudo,
|
|
221
|
+
wrappers: unwrapped.wrappers,
|
|
222
|
+
raw: node.text,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 映射重定向节点
|
|
227
|
+
*
|
|
228
|
+
* @param node - file_redirect 节点
|
|
229
|
+
* @returns 重定向信息;缺目标时返回 null
|
|
230
|
+
*/
|
|
231
|
+
function mapFileRedirect(node) {
|
|
232
|
+
let op = '';
|
|
233
|
+
let destination = null;
|
|
234
|
+
for (const child of node.children) {
|
|
235
|
+
// 操作符是匿名 token(>、>>、<、&>、>& 等),type 等于原文
|
|
236
|
+
if (child.namedChildren.length === 0 && /^[<>&]+$/.test(child.text)) {
|
|
237
|
+
op += child.text;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (child.type === 'file_descriptor')
|
|
241
|
+
continue;
|
|
242
|
+
destination = child;
|
|
243
|
+
}
|
|
244
|
+
if (!destination)
|
|
245
|
+
return null;
|
|
246
|
+
// fd 复制(`2>&1`、`>&2`):目标是 fd 号不是路径,沿用 fd:N 标记
|
|
247
|
+
const target = destination.type === 'number' && op.includes('&')
|
|
248
|
+
? `fd:${destination.text}`
|
|
249
|
+
: wordValue(destination);
|
|
250
|
+
let type;
|
|
251
|
+
if (op.includes('>>'))
|
|
252
|
+
type = 'append';
|
|
253
|
+
else if (op.includes('<'))
|
|
254
|
+
type = 'input';
|
|
255
|
+
else
|
|
256
|
+
type = 'overwrite';
|
|
257
|
+
return { type, target };
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* 提取词值:剥引号、去 bash 转义,插值/命令替换保留原文
|
|
261
|
+
*
|
|
262
|
+
* @param node - 词类节点(word/string/raw_string/number/expansion/…)
|
|
263
|
+
* @returns 风险评估用的词值
|
|
264
|
+
*/
|
|
265
|
+
function wordValue(node) {
|
|
266
|
+
switch (node.type) {
|
|
267
|
+
case 'ansi_c_string':
|
|
268
|
+
// $'...' ANSI-C 引用:剥掉 `$'` 与收尾 `'`,按转义规则还原
|
|
269
|
+
return unescapeToken(node.text.slice(2, -1));
|
|
270
|
+
case 'raw_string':
|
|
271
|
+
// 单引号内全字面量(含反斜杠),只剥外层引号
|
|
272
|
+
return node.text.slice(1, -1);
|
|
273
|
+
case 'string':
|
|
274
|
+
case 'concatenation': {
|
|
275
|
+
let out = '';
|
|
276
|
+
for (const child of node.namedChildren) {
|
|
277
|
+
out += wordValue(child);
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
case 'command_name':
|
|
282
|
+
case 'word':
|
|
283
|
+
case 'number': {
|
|
284
|
+
if (node.namedChildren.length > 0) {
|
|
285
|
+
let out = '';
|
|
286
|
+
for (const child of node.namedChildren) {
|
|
287
|
+
out += wordValue(child);
|
|
288
|
+
}
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
return unescapeToken(node.text);
|
|
292
|
+
}
|
|
293
|
+
case 'string_content':
|
|
294
|
+
return unescapeToken(node.text);
|
|
295
|
+
default:
|
|
296
|
+
// expansion / simple_expansion / command_substitution / process_substitution /
|
|
297
|
+
// arithmetic_expansion 等:保留原文(内联载荷探测按原文匹配)
|
|
298
|
+
return node.text;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// ── 连接符(按相邻命令段之间的原文定位) ──
|
|
302
|
+
/**
|
|
303
|
+
* 构建连接符列表
|
|
304
|
+
*
|
|
305
|
+
* @param raw - 原始命令
|
|
306
|
+
* @param spanned - 按源位置排序的命令段
|
|
307
|
+
* @returns 相邻命令段之间的连接符
|
|
308
|
+
*/
|
|
309
|
+
function buildConnectors(raw, spanned) {
|
|
310
|
+
const connectors = [];
|
|
311
|
+
for (let i = 0; i + 1 < spanned.length; i++) {
|
|
312
|
+
const prev = spanned[i];
|
|
313
|
+
const next = spanned[i + 1];
|
|
314
|
+
// 嵌套命令替换(下一段落在上一段源区间内)没有分隔原文,按 ; 处理
|
|
315
|
+
if (next.start < prev.end) {
|
|
316
|
+
connectors.push(';');
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const slice = raw.slice(prev.end, next.start);
|
|
320
|
+
const match = slice.match(/\|\||&&|[|;&\n]/);
|
|
321
|
+
connectors.push((match ? match[0] : '\n'));
|
|
322
|
+
}
|
|
323
|
+
return connectors;
|
|
324
|
+
}
|
|
325
|
+
// ── 特殊模式检测(结构判定) ──
|
|
326
|
+
function walk(node, visit) {
|
|
327
|
+
visit(node);
|
|
328
|
+
for (const child of node.namedChildren) {
|
|
329
|
+
walk(child, visit);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* 检测管道到解释器(管道最后一级是裸解释器)
|
|
334
|
+
*/
|
|
335
|
+
function detectShellPipe(root) {
|
|
336
|
+
let found = false;
|
|
337
|
+
walk(root, (n) => {
|
|
338
|
+
if (found || n.type !== 'pipeline')
|
|
339
|
+
return;
|
|
340
|
+
const named = n.namedChildren;
|
|
341
|
+
if (named.length < 2)
|
|
342
|
+
return;
|
|
343
|
+
// 末级可能被整句重定向包裹(`a | bash >log | …`):剥一层再看
|
|
344
|
+
const last = named[named.length - 1];
|
|
345
|
+
const target = last.type === 'redirected_statement'
|
|
346
|
+
? last.namedChildren.find((c) => c.type === 'command')
|
|
347
|
+
: last;
|
|
348
|
+
const seg = target && target.type === 'command' ? mapCommand(target) : null;
|
|
349
|
+
if (seg && INTERPRETER_NAMES.has(seg.command) && seg.args.length === 0) {
|
|
350
|
+
found = true;
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
return found;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* 检测子 shell($(...)、反引号、(...))
|
|
357
|
+
*
|
|
358
|
+
* 结构判定:单引号里的字面 `$(...)` 不会误报(fallback 的 raw 扫描会)。
|
|
359
|
+
*/
|
|
360
|
+
function detectSubshellNodes(root) {
|
|
361
|
+
let found = false;
|
|
362
|
+
walk(root, (n) => {
|
|
363
|
+
if (found)
|
|
364
|
+
return;
|
|
365
|
+
if (n.type === 'command_substitution' || n.type === 'subshell') {
|
|
366
|
+
found = true;
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
return found;
|
|
370
|
+
}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shell Parser —
|
|
2
|
+
* Shell Parser — 解析入口 facade
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* 3. 识别 >, >>, < 重定向 → 提取目标路径
|
|
9
|
-
* 4. 识别 sudo, env, exec 等 wrapper → 解包实际命令
|
|
4
|
+
* 主引擎:tree-sitter(shell-parser-treesitter.ts,vendored bash 语法 wasm);
|
|
5
|
+
* 回退引擎:纯字符串解析(shell-parser-fallback.ts,长期保留的降级防线,
|
|
6
|
+
* 保证主引擎缺席/解析报错时硬边界探测仍有输入)。
|
|
7
|
+
* 消费方(risk-evaluator / degradation)只依赖本模块导出,不感知引擎差异。
|
|
10
8
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* 回退语义:
|
|
10
|
+
* - 引擎未初始化(未走 Agent 构建引导的直接调用,如单元测试)→ fallback 结果;
|
|
11
|
+
* - tree-sitter 对某输入解析报错(语法树含 ERROR)或内部异常 → fallback 结果
|
|
12
|
+
* 并标记 `uncertain: true`,风险分档会附加「结构不确定」因子走 unknown 通道。
|
|
13
13
|
*/
|
|
14
14
|
import type { ParsedCommand } from './risk-types.js';
|
|
15
|
+
import { initShellParser, isShellParserReady, getShellParserEngine } from './shell-parser-treesitter.js';
|
|
16
|
+
export { initShellParser, isShellParserReady, getShellParserEngine };
|
|
17
|
+
export { parseShellCommandFallback } from './shell-parser-fallback.js';
|
|
15
18
|
/**
|
|
16
19
|
* 解析 Shell 命令
|
|
17
20
|
*
|
|
@@ -23,13 +26,23 @@ export declare function parseShellCommand(raw: string): ParsedCommand & {
|
|
|
23
26
|
};
|
|
24
27
|
/**
|
|
25
28
|
* 获取所有命令名(不含 wrapper)
|
|
29
|
+
*
|
|
30
|
+
* @param parsed - 解析结果
|
|
31
|
+
* @returns 命令名列表
|
|
26
32
|
*/
|
|
27
33
|
export declare function getCommandNames(parsed: ParsedCommand): string[];
|
|
28
34
|
/**
|
|
29
|
-
*
|
|
35
|
+
* 获取所有重定向目标路径(含未挂载到命令段的孤立重定向)
|
|
36
|
+
*
|
|
37
|
+
* @param parsed - 解析结果
|
|
38
|
+
* @returns 重定向目标列表
|
|
30
39
|
*/
|
|
31
40
|
export declare function getRedirectTargets(parsed: ParsedCommand): string[];
|
|
32
41
|
/**
|
|
33
42
|
* 判断命令是否包含指定的命令名
|
|
43
|
+
*
|
|
44
|
+
* @param parsed - 解析结果
|
|
45
|
+
* @param command - 命令名
|
|
46
|
+
* @returns 是否包含
|
|
34
47
|
*/
|
|
35
48
|
export declare function hasCommand(parsed: ParsedCommand, command: string): boolean;
|