skyloom 1.12.0 → 1.13.0

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.
Files changed (135) hide show
  1. package/.github/workflows/ci.yml +36 -36
  2. package/README.md +142 -46
  3. package/config/default.yaml +43 -47
  4. package/config/models.yaml +155 -155
  5. package/config/providers.yaml +39 -39
  6. package/config/skills/api_integrator/SKILL.md +15 -15
  7. package/config/skills/arch_designer/SKILL.md +13 -13
  8. package/config/skills/ci_cd_manager/SKILL.md +14 -14
  9. package/config/skills/code_analysis/SKILL.md +13 -13
  10. package/config/skills/code_generator/SKILL.md +12 -12
  11. package/config/skills/code_reviewer/SKILL.md +13 -13
  12. package/config/skills/content_writer/SKILL.md +14 -14
  13. package/config/skills/data_transformer/SKILL.md +15 -15
  14. package/config/skills/document_analysis/SKILL.md +13 -13
  15. package/config/skills/emotional_companion/SKILL.md +15 -15
  16. package/config/skills/performance_checker/SKILL.md +14 -14
  17. package/config/skills/security_auditor/SKILL.md +14 -14
  18. package/config/skills/self_evolve/SKILL.md +13 -13
  19. package/config/skills/sys_operator/SKILL.md +15 -15
  20. package/config/skills/task_planner/SKILL.md +14 -14
  21. package/config/skills/web_research/SKILL.md +14 -14
  22. package/config/skills/workflow_designer/SKILL.md +13 -13
  23. package/dist/agents/dew.js +52 -52
  24. package/dist/agents/fair.js +84 -84
  25. package/dist/agents/fog.js +30 -30
  26. package/dist/agents/frost.js +32 -32
  27. package/dist/agents/rain.js +32 -32
  28. package/dist/agents/snow.js +68 -68
  29. package/dist/cli/main.js +103 -51
  30. package/dist/cli/main.js.map +1 -1
  31. package/dist/cli/tui.d.ts.map +1 -1
  32. package/dist/cli/tui.js +8 -1
  33. package/dist/cli/tui.js.map +1 -1
  34. package/dist/core/agent/task.d.ts +58 -0
  35. package/dist/core/agent/task.d.ts.map +1 -0
  36. package/dist/core/agent/task.js +83 -0
  37. package/dist/core/agent/task.js.map +1 -0
  38. package/dist/core/agent.d.ts +2 -45
  39. package/dist/core/agent.d.ts.map +1 -1
  40. package/dist/core/agent.js +61 -145
  41. package/dist/core/agent.js.map +1 -1
  42. package/dist/core/agent_helpers.d.ts +10 -0
  43. package/dist/core/agent_helpers.d.ts.map +1 -1
  44. package/dist/core/agent_helpers.js +39 -0
  45. package/dist/core/agent_helpers.js.map +1 -1
  46. package/dist/core/catalog.d.ts +71 -0
  47. package/dist/core/catalog.d.ts.map +1 -0
  48. package/dist/core/catalog.js +176 -0
  49. package/dist/core/catalog.js.map +1 -0
  50. package/dist/core/config.d.ts +8 -0
  51. package/dist/core/config.d.ts.map +1 -1
  52. package/dist/core/config.js +12 -4
  53. package/dist/core/config.js.map +1 -1
  54. package/dist/core/factory.js +16 -16
  55. package/dist/core/llm.d.ts +7 -0
  56. package/dist/core/llm.d.ts.map +1 -1
  57. package/dist/core/llm.js +139 -7
  58. package/dist/core/llm.js.map +1 -1
  59. package/dist/core/longdoc.js +5 -5
  60. package/dist/core/memory.d.ts.map +1 -1
  61. package/dist/core/memory.js +69 -62
  62. package/dist/core/memory.js.map +1 -1
  63. package/dist/core/theme.d.ts +46 -0
  64. package/dist/core/theme.d.ts.map +1 -0
  65. package/dist/core/theme.js +42 -0
  66. package/dist/core/theme.js.map +1 -0
  67. package/dist/web/server.js +542 -519
  68. package/dist/web/server.js.map +1 -1
  69. package/docs/AESTHETIC_DESIGN.md +144 -0
  70. package/docs/OPTIMIZATION_PLAN.md +178 -0
  71. package/package.json +60 -60
  72. package/scripts/install.js +48 -48
  73. package/scripts/link.js +10 -10
  74. package/setup.bat +79 -79
  75. package/skill-test-ty2fOA/test.md +10 -10
  76. package/src/agents/dew.ts +70 -70
  77. package/src/agents/fair.ts +102 -102
  78. package/src/agents/fog.ts +48 -48
  79. package/src/agents/frost.ts +50 -50
  80. package/src/agents/rain.ts +50 -50
  81. package/src/agents/snow.ts +239 -239
  82. package/src/cli/main.ts +425 -372
  83. package/src/cli/mode.ts +58 -58
  84. package/src/cli/tui.ts +272 -269
  85. package/src/core/agent/task.ts +100 -0
  86. package/src/core/agent.ts +1446 -1549
  87. package/src/core/agent_helpers.ts +496 -461
  88. package/src/core/arbitrate.ts +162 -162
  89. package/src/core/catalog.ts +178 -0
  90. package/src/core/checkpoint.ts +94 -94
  91. package/src/core/config.ts +20 -4
  92. package/src/core/estimate.ts +104 -104
  93. package/src/core/evolve.ts +191 -191
  94. package/src/core/factory.ts +627 -627
  95. package/src/core/filter.ts +103 -103
  96. package/src/core/graph.ts +156 -156
  97. package/src/core/icons.ts +53 -53
  98. package/src/core/index.ts +37 -37
  99. package/src/core/learn.ts +146 -146
  100. package/src/core/llm.ts +108 -5
  101. package/src/core/longdoc.ts +155 -155
  102. package/src/core/mcp_server.ts +176 -176
  103. package/src/core/memory.ts +1178 -1171
  104. package/src/core/profile.ts +255 -255
  105. package/src/core/router.ts +124 -124
  106. package/src/core/sandbox.ts +142 -142
  107. package/src/core/security.ts +243 -243
  108. package/src/core/skill.ts +342 -342
  109. package/src/core/theme.ts +65 -0
  110. package/src/core/tool_router.ts +193 -193
  111. package/src/core/vector.ts +152 -152
  112. package/src/core/workspace.ts +150 -150
  113. package/src/plugins/loader.ts +66 -66
  114. package/src/skills/loader.ts +46 -46
  115. package/src/sql.js.d.ts +29 -29
  116. package/src/tools/builtin.ts +380 -380
  117. package/src/tools/computer.ts +269 -269
  118. package/src/tools/delegate.ts +49 -49
  119. package/src/web/server.ts +660 -634
  120. package/src/web/tts.ts +93 -93
  121. package/tests/agent_helpers.test.ts +48 -0
  122. package/tests/bus.test.ts +121 -121
  123. package/tests/catalog.test.ts +86 -0
  124. package/tests/config.test.ts +41 -0
  125. package/tests/icons.test.ts +45 -45
  126. package/tests/memory.test.ts +147 -0
  127. package/tests/router.test.ts +86 -86
  128. package/tests/schemas.test.ts +51 -51
  129. package/tests/semantic.test.ts +83 -83
  130. package/tests/setup.ts +10 -10
  131. package/tests/skill.test.ts +172 -172
  132. package/tests/task.test.ts +60 -0
  133. package/tests/tool.test.ts +108 -108
  134. package/tests/tool_router.test.ts +71 -71
  135. package/vitest.config.ts +17 -17
@@ -1,124 +1,124 @@
1
- /**
2
- * Complexity router: classify a user goal into direct / single / orchestrate.
3
- *
4
- * Rules-first (no LLM), so classification stays under 1ms. The router exists
5
- * solely to keep simple goals from triggering Snow's full task-decomposition
6
- * LLM call when a single agent could answer in one shot.
7
- */
8
-
9
- export type Mode = 'direct' | 'single' | 'orchestrate';
10
-
11
- const MULTI_STEP_TOKENS = [
12
- '先', '再', '然后', '接着', '之后', '其次', '最后',
13
- '第一步', '第二步', '第三步', '步骤', '顺序', '依次', '首先',
14
- 'step 1', 'step 2', 'first,', 'then,', 'after that', 'finally',
15
- ];
16
-
17
- const GREETING_TOKENS = [
18
- '你好', '您好', 'hi', 'hello', 'hey', '在吗', '嗨',
19
- '早上好', '晚安', '谢谢', 'thanks', 'thank you', '再见', 'bye',
20
- ];
21
-
22
- const SINGLE_ACTION_HINTS = [
23
- '解释', '什么是', '为什么', '如何', '怎么', '查询', '搜索',
24
- '搜一下', '翻译', '总结', 'summarize', 'explain', 'what is', 'why', 'how do',
25
- ];
26
-
27
- const ACTION_VERBS = [
28
- '写', '帮我写', '生成', '创建', '实现', '做', '搜', '查',
29
- '找', '审查', '审计', '翻译', '重构', '修改', '改', '部署',
30
- '运行', '执行',
31
- 'write', 'create', 'generate', 'implement', 'search', 'find',
32
- 'review', 'translate', 'deploy', 'run',
33
- ];
34
-
35
- const CODE_BLOCK = /```[\s\S]+?```/;
36
- const URL_PATTERN = /https?:\/\/\S+/;
37
- const PATH_PATTERN = /(?:[A-Za-z]:[\\/]|[\\/])[\w\-./\\]+/;
38
- const NUMBERED_LIST = /(?:^|\n)\s*(?:\d+[.)、]|[-*])\s+/gm;
39
- const INLINE_ENUMERATED = /\b\d+[.)、]\s*\S/g;
40
-
41
- /**
42
- * Decide the execution mode for a user goal.
43
- *
44
- * direct: short greeting / single factual question, no tools needed.
45
- * single: clear single-purpose task, one agent + tools.
46
- * orchestrate: multi-step plan worth decomposing into sub-tasks.
47
- */
48
- export function classify(goal: string): Mode {
49
- if (!goal || !goal.trim()) return 'direct';
50
-
51
- const text = goal.trim();
52
- const lower = text.toLowerCase();
53
- const length = text.length;
54
-
55
- const hasCode = CODE_BLOCK.test(text);
56
- const hasUrl = URL_PATTERN.test(text);
57
- const hasPath = PATH_PATTERN.test(text);
58
- const listMatches = (text.match(NUMBERED_LIST) || []).length;
59
- const inlineEnumHits = (text.match(INLINE_ENUMERATED) || []).length;
60
-
61
- const multiStepHits = MULTI_STEP_TOKENS.filter(t => lower.includes(t)).length;
62
- const greetingHits = GREETING_TOKENS.filter(t => lower.includes(t)).length;
63
- const singleHits = SINGLE_ACTION_HINTS.filter(t => lower.includes(t)).length;
64
- const actionHits = ACTION_VERBS.filter(t => lower.includes(t)).length;
65
-
66
- if (greetingHits >= 1 && length < 30 && multiStepHits === 0 && actionHits === 0) {
67
- return 'direct';
68
- }
69
-
70
- if (multiStepHits >= 2 || listMatches >= 2 || inlineEnumHits >= 3) {
71
- return 'orchestrate';
72
- }
73
-
74
- if (length > 200 && multiStepHits >= 1) {
75
- return 'orchestrate';
76
- }
77
-
78
- if (hasCode && length > 150) {
79
- return 'orchestrate';
80
- }
81
-
82
- // Tool-use signals push toward single, not direct
83
- if (hasPath || hasUrl || actionHits >= 1) {
84
- return 'single';
85
- }
86
-
87
- if (length < 50 && !hasCode) {
88
- if (singleHits >= 1 || text.endsWith('?') || text.endsWith('?')) {
89
- return 'direct';
90
- }
91
- if (multiStepHits === 0) {
92
- return 'direct';
93
- }
94
- }
95
-
96
- return 'single';
97
- }
98
-
99
- /**
100
- * Pick a single agent for a non-orchestrate goal, by keyword routing.
101
- *
102
- * Returns an agent name guaranteed to be in available, falling back to
103
- * rain (generation generalist) then to any available agent.
104
- */
105
- export function pickAgentForGoal(goal: string, available: Set<string>): string {
106
- const lower = goal.toLowerCase();
107
-
108
- // More specific buckets first
109
- const buckets: Array<[string, string[]]> = [
110
- ['frost', ['审查', 'review', '漏洞', '安全', '审计', 'lint', '重构建议', 'code smell']],
111
- ['dew', ['部署', '运行', '执行命令', 'shell', 'deploy', 'ci', 'cd', '环境变量', '运维']],
112
- ['fog', ['研究', '调研', '搜一下', '搜索', '查一下', '查资料', 'research', 'search', '调查', '找一下', '找资料']],
113
- ['rain', ['写', '生成', '实现', 'create', 'generate', '写一段', '写个', '代码', '函数', '实现一个']],
114
- ['fair', ['陪我', '聊天', '心情', '难过', '开心', '孤独', '倾诉', '你好', 'hi', 'hello', '嗨']],
115
- ];
116
-
117
- for (const [agent, hints] of buckets) {
118
- if (!available.has(agent)) continue;
119
- if (hints.some(h => lower.includes(h))) return agent;
120
- }
121
-
122
- if (available.has('rain')) return 'rain';
123
- return Array.from(available)[0];
124
- }
1
+ /**
2
+ * Complexity router: classify a user goal into direct / single / orchestrate.
3
+ *
4
+ * Rules-first (no LLM), so classification stays under 1ms. The router exists
5
+ * solely to keep simple goals from triggering Snow's full task-decomposition
6
+ * LLM call when a single agent could answer in one shot.
7
+ */
8
+
9
+ export type Mode = 'direct' | 'single' | 'orchestrate';
10
+
11
+ const MULTI_STEP_TOKENS = [
12
+ '先', '再', '然后', '接着', '之后', '其次', '最后',
13
+ '第一步', '第二步', '第三步', '步骤', '顺序', '依次', '首先',
14
+ 'step 1', 'step 2', 'first,', 'then,', 'after that', 'finally',
15
+ ];
16
+
17
+ const GREETING_TOKENS = [
18
+ '你好', '您好', 'hi', 'hello', 'hey', '在吗', '嗨',
19
+ '早上好', '晚安', '谢谢', 'thanks', 'thank you', '再见', 'bye',
20
+ ];
21
+
22
+ const SINGLE_ACTION_HINTS = [
23
+ '解释', '什么是', '为什么', '如何', '怎么', '查询', '搜索',
24
+ '搜一下', '翻译', '总结', 'summarize', 'explain', 'what is', 'why', 'how do',
25
+ ];
26
+
27
+ const ACTION_VERBS = [
28
+ '写', '帮我写', '生成', '创建', '实现', '做', '搜', '查',
29
+ '找', '审查', '审计', '翻译', '重构', '修改', '改', '部署',
30
+ '运行', '执行',
31
+ 'write', 'create', 'generate', 'implement', 'search', 'find',
32
+ 'review', 'translate', 'deploy', 'run',
33
+ ];
34
+
35
+ const CODE_BLOCK = /```[\s\S]+?```/;
36
+ const URL_PATTERN = /https?:\/\/\S+/;
37
+ const PATH_PATTERN = /(?:[A-Za-z]:[\\/]|[\\/])[\w\-./\\]+/;
38
+ const NUMBERED_LIST = /(?:^|\n)\s*(?:\d+[.)、]|[-*])\s+/gm;
39
+ const INLINE_ENUMERATED = /\b\d+[.)、]\s*\S/g;
40
+
41
+ /**
42
+ * Decide the execution mode for a user goal.
43
+ *
44
+ * direct: short greeting / single factual question, no tools needed.
45
+ * single: clear single-purpose task, one agent + tools.
46
+ * orchestrate: multi-step plan worth decomposing into sub-tasks.
47
+ */
48
+ export function classify(goal: string): Mode {
49
+ if (!goal || !goal.trim()) return 'direct';
50
+
51
+ const text = goal.trim();
52
+ const lower = text.toLowerCase();
53
+ const length = text.length;
54
+
55
+ const hasCode = CODE_BLOCK.test(text);
56
+ const hasUrl = URL_PATTERN.test(text);
57
+ const hasPath = PATH_PATTERN.test(text);
58
+ const listMatches = (text.match(NUMBERED_LIST) || []).length;
59
+ const inlineEnumHits = (text.match(INLINE_ENUMERATED) || []).length;
60
+
61
+ const multiStepHits = MULTI_STEP_TOKENS.filter(t => lower.includes(t)).length;
62
+ const greetingHits = GREETING_TOKENS.filter(t => lower.includes(t)).length;
63
+ const singleHits = SINGLE_ACTION_HINTS.filter(t => lower.includes(t)).length;
64
+ const actionHits = ACTION_VERBS.filter(t => lower.includes(t)).length;
65
+
66
+ if (greetingHits >= 1 && length < 30 && multiStepHits === 0 && actionHits === 0) {
67
+ return 'direct';
68
+ }
69
+
70
+ if (multiStepHits >= 2 || listMatches >= 2 || inlineEnumHits >= 3) {
71
+ return 'orchestrate';
72
+ }
73
+
74
+ if (length > 200 && multiStepHits >= 1) {
75
+ return 'orchestrate';
76
+ }
77
+
78
+ if (hasCode && length > 150) {
79
+ return 'orchestrate';
80
+ }
81
+
82
+ // Tool-use signals push toward single, not direct
83
+ if (hasPath || hasUrl || actionHits >= 1) {
84
+ return 'single';
85
+ }
86
+
87
+ if (length < 50 && !hasCode) {
88
+ if (singleHits >= 1 || text.endsWith('?') || text.endsWith('?')) {
89
+ return 'direct';
90
+ }
91
+ if (multiStepHits === 0) {
92
+ return 'direct';
93
+ }
94
+ }
95
+
96
+ return 'single';
97
+ }
98
+
99
+ /**
100
+ * Pick a single agent for a non-orchestrate goal, by keyword routing.
101
+ *
102
+ * Returns an agent name guaranteed to be in available, falling back to
103
+ * rain (generation generalist) then to any available agent.
104
+ */
105
+ export function pickAgentForGoal(goal: string, available: Set<string>): string {
106
+ const lower = goal.toLowerCase();
107
+
108
+ // More specific buckets first
109
+ const buckets: Array<[string, string[]]> = [
110
+ ['frost', ['审查', 'review', '漏洞', '安全', '审计', 'lint', '重构建议', 'code smell']],
111
+ ['dew', ['部署', '运行', '执行命令', 'shell', 'deploy', 'ci', 'cd', '环境变量', '运维']],
112
+ ['fog', ['研究', '调研', '搜一下', '搜索', '查一下', '查资料', 'research', 'search', '调查', '找一下', '找资料']],
113
+ ['rain', ['写', '生成', '实现', 'create', 'generate', '写一段', '写个', '代码', '函数', '实现一个']],
114
+ ['fair', ['陪我', '聊天', '心情', '难过', '开心', '孤独', '倾诉', '你好', 'hi', 'hello', '嗨']],
115
+ ];
116
+
117
+ for (const [agent, hints] of buckets) {
118
+ if (!available.has(agent)) continue;
119
+ if (hints.some(h => lower.includes(h))) return agent;
120
+ }
121
+
122
+ if (available.has('rain')) return 'rain';
123
+ return Array.from(available)[0];
124
+ }
@@ -1,142 +1,142 @@
1
- /**
2
- * 沙箱隔离模块 — Shell execution sandbox with resource limits.
3
- *
4
- * All `run_bash` / `shell_exec` commands are wrapped through this module
5
- * to ensure: temp directory isolation, timeout enforcement, output size
6
- * limits, and dangerous command detection BEFORE execution.
7
- */
8
-
9
- import { execSync, exec } from "child_process";
10
- import * as fs from "fs";
11
- import * as path from "path";
12
- import * as os from "os";
13
- import { getLogger } from "./logger";
14
- import { REDLINE_PATTERNS, REDLINE_COMMANDS } from "./security";
15
-
16
- const log = getLogger("sandbox");
17
-
18
- /* ═══════════════════════════════════════
19
- Configuration
20
- ═══════════════════════════════════════ */
21
- const SANDBOX_DIR = path.join(os.homedir(), ".skyloom", "sandbox");
22
- const DEFAULT_TIMEOUT_MS = 30000; // 30s max
23
- const MAX_OUTPUT_BYTES = 1024 * 1024; // 1MB max output
24
- const HARD_TIMEOUT_MS = 120000; // 2min absolute max
25
- // Whitelist of safe commands that don't need sandbox
26
- const SAFE_COMMANDS = new Set(["echo", "pwd", "whoami", "date", "hostname", "uname", "ls", "dir", "cat", "head", "tail", "wc", "env", "printenv"]);
27
-
28
- /* ═══════════════════════════════════════
29
- Sandbox lifecycle
30
- ═══════════════════════════════════════ */
31
- function ensureSandbox(): string {
32
- if (!fs.existsSync(SANDBOX_DIR)) fs.mkdirSync(SANDBOX_DIR, { recursive: true });
33
- // Create named temp dir for this execution
34
- const id = Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
35
- const dir = path.join(SANDBOX_DIR, `job_${id}`);
36
- fs.mkdirSync(dir, { recursive: true });
37
- return dir;
38
- }
39
-
40
- function cleanup(dir: string): void {
41
- try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
42
- }
43
-
44
- /* ═══════════════════════════════════════
45
- Pre-execution check
46
- ═══════════════════════════════════════ */
47
- function preflightCheck(command: string): string | null {
48
- if (!command || !command.trim()) return "Empty command";
49
-
50
- const lower = command.toLowerCase().trim();
51
-
52
- // Red-line patterns
53
- for (const pattern of REDLINE_PATTERNS) {
54
- if (pattern.test(lower)) return `REDLINE: pattern '${pattern.source.slice(0, 40)}' detected`;
55
- }
56
- for (const forbidden of REDLINE_COMMANDS) {
57
- if (lower.includes(forbidden)) return `REDLINE: forbidden command '${forbidden}'`;
58
- }
59
-
60
- // Network exfiltration attempts
61
- if (/curl.*\|.*nc\s/.test(lower) || /wget.*-O.*>/.test(lower)) return "BLOCKED: potential data exfiltration";
62
- if (/nc\s+\S+\s+\d+/.test(lower) && /\|/.test(lower)) return "BLOCKED: potential reverse shell";
63
-
64
- return null;
65
- }
66
-
67
- /* ═══════════════════════════════════════
68
- Execute in sandbox
69
- ═══════════════════════════════════════ */
70
- export interface SandboxResult {
71
- success: boolean;
72
- stdout: string;
73
- stderr: string;
74
- exitCode: number;
75
- killed: boolean;
76
- durationMs: number;
77
- sandboxDir: string;
78
- checkFailed?: string;
79
- }
80
-
81
- export function runInSandbox(command: string, opts?: {
82
- timeoutMs?: number;
83
- cwd?: string;
84
- env?: Record<string, string>;
85
- }): SandboxResult {
86
- // Pre-flight
87
- const check = preflightCheck(command);
88
- if (check) {
89
- return { success: false, stdout: "", stderr: check, exitCode: -1, killed: false, durationMs: 0, sandboxDir: "", checkFailed: check };
90
- }
91
-
92
- const dir = ensureSandbox();
93
- const timeout = Math.min(opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS, HARD_TIMEOUT_MS);
94
- const t0 = Date.now();
95
-
96
- try {
97
- // For safe commands, run in-place without sandbox overhead
98
- const firstWord = command.trim().split(/\s+/)[0].toLowerCase();
99
- if (SAFE_COMMANDS.has(firstWord)) {
100
- const result = execSync(command, { encoding: "utf-8", timeout, maxBuffer: MAX_OUTPUT_BYTES, cwd: opts?.cwd || dir, env: { ...process.env, ...(opts?.env || {}) } });
101
- cleanup(dir);
102
- return { success: true, stdout: result.slice(0, MAX_OUTPUT_BYTES), stderr: "", exitCode: 0, killed: false, durationMs: Date.now() - t0, sandboxDir: dir };
103
- }
104
-
105
- // Dangerous command — run in sandbox with isolation
106
- const result = execSync(command, {
107
- encoding: "utf-8",
108
- timeout,
109
- maxBuffer: MAX_OUTPUT_BYTES,
110
- cwd: dir, // isolate to temp dir
111
- env: { ...process.env, ...(opts?.env || {}), TMPDIR: dir, TEMP: dir },
112
- windowsHide: true,
113
- });
114
-
115
- cleanup(dir);
116
- return { success: true, stdout: result.slice(0, MAX_OUTPUT_BYTES), stderr: "", exitCode: 0, killed: false, durationMs: Date.now() - t0, sandboxDir: dir };
117
-
118
- } catch (e: any) {
119
- const durationMs = Date.now() - t0;
120
- const killed = e.killed || e.signal !== undefined || durationMs >= timeout;
121
- const stdout = (e.stdout || "").slice(0, MAX_OUTPUT_BYTES);
122
- const stderr = (e.stderr || e.message || "").slice(0, MAX_OUTPUT_BYTES);
123
- cleanup(dir);
124
- return { success: false, stdout, stderr, exitCode: e.status || -1, killed, durationMs, sandboxDir: dir };
125
- }
126
- }
127
-
128
- /* ═══════════════════════════════════════
129
- Format result for display
130
- ═══════════════════════════════════════ */
131
- export function formatSandboxResult(r: SandboxResult): string {
132
- if (r.checkFailed) return `[BLOCKED] ${r.checkFailed}`;
133
-
134
- const parts: string[] = [];
135
- if (r.stdout) parts.push(r.stdout);
136
- if (r.stderr) parts.push(`[stderr]\n${r.stderr}`);
137
- if (r.killed) parts.push(`[killed after ${r.durationMs}ms]`);
138
- if (r.exitCode !== 0) parts.push(`[exit code: ${r.exitCode}]`);
139
-
140
- parts.push(`[sandbox: ${r.sandboxDir || "n/a"} · ${r.durationMs}ms]`);
141
- return parts.join("\n");
142
- }
1
+ /**
2
+ * 沙箱隔离模块 — Shell execution sandbox with resource limits.
3
+ *
4
+ * All `run_bash` / `shell_exec` commands are wrapped through this module
5
+ * to ensure: temp directory isolation, timeout enforcement, output size
6
+ * limits, and dangerous command detection BEFORE execution.
7
+ */
8
+
9
+ import { execSync, exec } from "child_process";
10
+ import * as fs from "fs";
11
+ import * as path from "path";
12
+ import * as os from "os";
13
+ import { getLogger } from "./logger";
14
+ import { REDLINE_PATTERNS, REDLINE_COMMANDS } from "./security";
15
+
16
+ const log = getLogger("sandbox");
17
+
18
+ /* ═══════════════════════════════════════
19
+ Configuration
20
+ ═══════════════════════════════════════ */
21
+ const SANDBOX_DIR = path.join(os.homedir(), ".skyloom", "sandbox");
22
+ const DEFAULT_TIMEOUT_MS = 30000; // 30s max
23
+ const MAX_OUTPUT_BYTES = 1024 * 1024; // 1MB max output
24
+ const HARD_TIMEOUT_MS = 120000; // 2min absolute max
25
+ // Whitelist of safe commands that don't need sandbox
26
+ const SAFE_COMMANDS = new Set(["echo", "pwd", "whoami", "date", "hostname", "uname", "ls", "dir", "cat", "head", "tail", "wc", "env", "printenv"]);
27
+
28
+ /* ═══════════════════════════════════════
29
+ Sandbox lifecycle
30
+ ═══════════════════════════════════════ */
31
+ function ensureSandbox(): string {
32
+ if (!fs.existsSync(SANDBOX_DIR)) fs.mkdirSync(SANDBOX_DIR, { recursive: true });
33
+ // Create named temp dir for this execution
34
+ const id = Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
35
+ const dir = path.join(SANDBOX_DIR, `job_${id}`);
36
+ fs.mkdirSync(dir, { recursive: true });
37
+ return dir;
38
+ }
39
+
40
+ function cleanup(dir: string): void {
41
+ try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
42
+ }
43
+
44
+ /* ═══════════════════════════════════════
45
+ Pre-execution check
46
+ ═══════════════════════════════════════ */
47
+ function preflightCheck(command: string): string | null {
48
+ if (!command || !command.trim()) return "Empty command";
49
+
50
+ const lower = command.toLowerCase().trim();
51
+
52
+ // Red-line patterns
53
+ for (const pattern of REDLINE_PATTERNS) {
54
+ if (pattern.test(lower)) return `REDLINE: pattern '${pattern.source.slice(0, 40)}' detected`;
55
+ }
56
+ for (const forbidden of REDLINE_COMMANDS) {
57
+ if (lower.includes(forbidden)) return `REDLINE: forbidden command '${forbidden}'`;
58
+ }
59
+
60
+ // Network exfiltration attempts
61
+ if (/curl.*\|.*nc\s/.test(lower) || /wget.*-O.*>/.test(lower)) return "BLOCKED: potential data exfiltration";
62
+ if (/nc\s+\S+\s+\d+/.test(lower) && /\|/.test(lower)) return "BLOCKED: potential reverse shell";
63
+
64
+ return null;
65
+ }
66
+
67
+ /* ═══════════════════════════════════════
68
+ Execute in sandbox
69
+ ═══════════════════════════════════════ */
70
+ export interface SandboxResult {
71
+ success: boolean;
72
+ stdout: string;
73
+ stderr: string;
74
+ exitCode: number;
75
+ killed: boolean;
76
+ durationMs: number;
77
+ sandboxDir: string;
78
+ checkFailed?: string;
79
+ }
80
+
81
+ export function runInSandbox(command: string, opts?: {
82
+ timeoutMs?: number;
83
+ cwd?: string;
84
+ env?: Record<string, string>;
85
+ }): SandboxResult {
86
+ // Pre-flight
87
+ const check = preflightCheck(command);
88
+ if (check) {
89
+ return { success: false, stdout: "", stderr: check, exitCode: -1, killed: false, durationMs: 0, sandboxDir: "", checkFailed: check };
90
+ }
91
+
92
+ const dir = ensureSandbox();
93
+ const timeout = Math.min(opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS, HARD_TIMEOUT_MS);
94
+ const t0 = Date.now();
95
+
96
+ try {
97
+ // For safe commands, run in-place without sandbox overhead
98
+ const firstWord = command.trim().split(/\s+/)[0].toLowerCase();
99
+ if (SAFE_COMMANDS.has(firstWord)) {
100
+ const result = execSync(command, { encoding: "utf-8", timeout, maxBuffer: MAX_OUTPUT_BYTES, cwd: opts?.cwd || dir, env: { ...process.env, ...(opts?.env || {}) } });
101
+ cleanup(dir);
102
+ return { success: true, stdout: result.slice(0, MAX_OUTPUT_BYTES), stderr: "", exitCode: 0, killed: false, durationMs: Date.now() - t0, sandboxDir: dir };
103
+ }
104
+
105
+ // Dangerous command — run in sandbox with isolation
106
+ const result = execSync(command, {
107
+ encoding: "utf-8",
108
+ timeout,
109
+ maxBuffer: MAX_OUTPUT_BYTES,
110
+ cwd: dir, // isolate to temp dir
111
+ env: { ...process.env, ...(opts?.env || {}), TMPDIR: dir, TEMP: dir },
112
+ windowsHide: true,
113
+ });
114
+
115
+ cleanup(dir);
116
+ return { success: true, stdout: result.slice(0, MAX_OUTPUT_BYTES), stderr: "", exitCode: 0, killed: false, durationMs: Date.now() - t0, sandboxDir: dir };
117
+
118
+ } catch (e: any) {
119
+ const durationMs = Date.now() - t0;
120
+ const killed = e.killed || e.signal !== undefined || durationMs >= timeout;
121
+ const stdout = (e.stdout || "").slice(0, MAX_OUTPUT_BYTES);
122
+ const stderr = (e.stderr || e.message || "").slice(0, MAX_OUTPUT_BYTES);
123
+ cleanup(dir);
124
+ return { success: false, stdout, stderr, exitCode: e.status || -1, killed, durationMs, sandboxDir: dir };
125
+ }
126
+ }
127
+
128
+ /* ═══════════════════════════════════════
129
+ Format result for display
130
+ ═══════════════════════════════════════ */
131
+ export function formatSandboxResult(r: SandboxResult): string {
132
+ if (r.checkFailed) return `[BLOCKED] ${r.checkFailed}`;
133
+
134
+ const parts: string[] = [];
135
+ if (r.stdout) parts.push(r.stdout);
136
+ if (r.stderr) parts.push(`[stderr]\n${r.stderr}`);
137
+ if (r.killed) parts.push(`[killed after ${r.durationMs}ms]`);
138
+ if (r.exitCode !== 0) parts.push(`[exit code: ${r.exitCode}]`);
139
+
140
+ parts.push(`[sandbox: ${r.sandboxDir || "n/a"} · ${r.durationMs}ms]`);
141
+ return parts.join("\n");
142
+ }