skyloom 1.5.3 → 1.7.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 (41) hide show
  1. package/dist/core/agent.d.ts +2 -0
  2. package/dist/core/agent.d.ts.map +1 -1
  3. package/dist/core/agent.js +33 -6
  4. package/dist/core/agent.js.map +1 -1
  5. package/dist/core/arbitrate.d.ts +32 -0
  6. package/dist/core/arbitrate.d.ts.map +1 -0
  7. package/dist/core/arbitrate.js +136 -0
  8. package/dist/core/arbitrate.js.map +1 -0
  9. package/dist/core/estimate.d.ts +30 -0
  10. package/dist/core/estimate.d.ts.map +1 -0
  11. package/dist/core/estimate.js +94 -0
  12. package/dist/core/estimate.js.map +1 -0
  13. package/dist/core/filter.d.ts +16 -0
  14. package/dist/core/filter.d.ts.map +1 -0
  15. package/dist/core/filter.js +91 -0
  16. package/dist/core/filter.js.map +1 -0
  17. package/dist/core/index.d.ts +7 -1
  18. package/dist/core/index.d.ts.map +1 -1
  19. package/dist/core/index.js +13 -2
  20. package/dist/core/index.js.map +1 -1
  21. package/dist/core/learn.d.ts +30 -0
  22. package/dist/core/learn.d.ts.map +1 -0
  23. package/dist/core/learn.js +156 -0
  24. package/dist/core/learn.js.map +1 -0
  25. package/dist/core/longdoc.d.ts +41 -0
  26. package/dist/core/longdoc.d.ts.map +1 -0
  27. package/dist/core/longdoc.js +128 -0
  28. package/dist/core/longdoc.js.map +1 -0
  29. package/dist/core/security.d.ts +73 -0
  30. package/dist/core/security.d.ts.map +1 -0
  31. package/dist/core/security.js +220 -0
  32. package/dist/core/security.js.map +1 -0
  33. package/package.json +1 -1
  34. package/src/core/agent.ts +18 -6
  35. package/src/core/arbitrate.ts +162 -0
  36. package/src/core/estimate.ts +104 -0
  37. package/src/core/filter.ts +103 -0
  38. package/src/core/index.ts +8 -2
  39. package/src/core/learn.ts +146 -0
  40. package/src/core/longdoc.ts +155 -0
  41. package/src/core/security.ts +243 -0
@@ -0,0 +1,103 @@
1
+ /**
2
+ * 输出过滤模块 — sensitive information sanitization.
3
+ *
4
+ * Before agent responses reach the user (or are persisted),
5
+ * scan for and redact sensitive patterns like API keys,
6
+ * tokens, passwords, PII, and internal paths.
7
+ */
8
+
9
+ /* ═══════════════════════════════════════
10
+ Detection patterns — compiled once at module load
11
+ ═══════════════════════════════════════ */
12
+ const SENSITIVE_PATTERNS: Array<[RegExp, string]> = [
13
+ // API keys & tokens
14
+ [/sk-[a-zA-Z0-9]{32,}/g, "[REDACTED:API_KEY]"],
15
+ [/(?:api_key|apikey|secret_key|access_token|auth_token)\s*[:=]\s*["']?[^\s"']{8,}["']?/gi, "$1: [REDACTED]"],
16
+ [/ghp_[a-zA-Z0-9]{36}/g, "[REDACTED:GITHUB_TOKEN]"],
17
+ [/gho_[a-zA-Z0-9]{36}/g, "[REDACTED:GITHUB_TOKEN]"],
18
+
19
+ // AWS credentials
20
+ [/AKIA[0-9A-Z]{16}/g, "[REDACTED:AWS_KEY]"],
21
+ [/(?:aws_access_key_id|aws_secret_access_key)\s*[:=]\s*["']?[^\s"']+/gi, "$1: [REDACTED]"],
22
+
23
+ // Passwords
24
+ [/(?:password|passwd|pwd)\s*[:=]\s*["']?[^\s"']{4,}["']?/gi, "$1: [REDACTED]"],
25
+ [/(?:密码|口令)\s*[:=]\s*["']?[^\s"']{2,}["']?/g, "$1: [已脱敏]"],
26
+
27
+ // Connection strings
28
+ [/(?:mongodb|postgres|mysql|redis):\/\/[^\s]+/g, "[REDACTED:DB_URI]"],
29
+ [/(?:jdbc|odbc):[^\s]+/g, "[REDACTED:DB_URI]"],
30
+
31
+ // Private keys
32
+ [/-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END .*?PRIVATE KEY-----/g, "[REDACTED:PRIVATE_KEY]"],
33
+
34
+ // IP addresses (local only)
35
+ [/192\.168\.\d{1,3}\.\d{1,3}/g, "[REDACTED:LAN_IP]"],
36
+ [/10\.\d{1,3}\.\d{1,3}\.\d{1,3}/g, "[REDACTED:LAN_IP]"],
37
+ [/172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}/g, "[REDACTED:LAN_IP]"],
38
+
39
+ // File paths
40
+ [/(?:\/etc\/(?:passwd|shadow|hosts|sudoers))/g, "[REDACTED:SYSTEM_PATH]"],
41
+ ];
42
+
43
+ /* Email masking (function-based, handled separately) */
44
+ const EMAIL_RE = /([a-zA-Z0-9._%+-]{3,})@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
45
+
46
+
47
+ /* ═══════════════════════════════════════
48
+ Filter function
49
+ ═══════════════════════════════════════ */
50
+ export interface FilterResult {
51
+ clean: string;
52
+ redacted: boolean;
53
+ count: number;
54
+ details: string[];
55
+ }
56
+
57
+ export function filterOutput(text: string): FilterResult {
58
+ if (!text) return { clean: "", redacted: false, count: 0, details: [] };
59
+
60
+ let clean = text;
61
+ let count = 0;
62
+ const details: string[] = [];
63
+
64
+ // Email masking (function-based replacement)
65
+ let emailCount = 0;
66
+ clean = clean.replace(EMAIL_RE, (full, user, domain) => {
67
+ emailCount++;
68
+ return (user as string).slice(0, 2) + "***@" + (domain as string);
69
+ });
70
+ if (emailCount > 0) {
71
+ count += emailCount;
72
+ details.push(`Masked ${emailCount}x email addresses`);
73
+ }
74
+
75
+ for (const [pattern, replacement] of SENSITIVE_PATTERNS) {
76
+ const matches = clean.match(pattern);
77
+ if (matches) {
78
+ count += matches.length;
79
+ if (typeof replacement === "string") {
80
+ details.push(`Redacted ${matches.length}x ${pattern.source.slice(0, 30)}`);
81
+ } else {
82
+ details.push(`Masked ${matches.length}x email addresses`);
83
+ }
84
+ clean = clean.replace(pattern, replacement as string);
85
+ }
86
+ }
87
+
88
+ return { clean, redacted: count > 0, count, details };
89
+ }
90
+
91
+ /* ═══════════════════════════════════════
92
+ Quick check — is filtering needed?
93
+ ═══════════════════════════════════════ */
94
+ export function needsFiltering(text: string): boolean {
95
+ if (!text) return false;
96
+ // Quick scan with the most common patterns
97
+ if (/sk-[a-zA-Z0-9]{32,}/.test(text)) return true;
98
+ if (/api_key.*[:=]/.test(text)) return true;
99
+ if (/password.*[:=]/.test(text)) return true;
100
+ if (/-----BEGIN.*PRIVATE KEY-----/.test(text)) return true;
101
+ if (EMAIL_RE.test(text)) return true;
102
+ return false;
103
+ }
package/src/core/index.ts CHANGED
@@ -26,6 +26,12 @@ export * from './skill';
26
26
  export * from './router';
27
27
  export * from './agent';
28
28
  export * from './factory';
29
+ export * from './security';
30
+ export * from './learn';
31
+ export * from './longdoc';
32
+ export * from './filter';
33
+ export * from './estimate';
34
+ export * from './arbitrate';
29
35
 
30
- // Version
31
- export const VERSION = '1.4.0';
36
+ // Version — read from package.json
37
+ export const VERSION = (() => { try { return require('../../package.json').version; } catch { return '1.6.0'; } })();
@@ -0,0 +1,146 @@
1
+ /**
2
+ * 持续学习模块 — post-task review + experience recording.
3
+ *
4
+ * After each task, the agent writes a structured review.
5
+ * Failed attempts are indexed for similarity search to avoid repetition.
6
+ */
7
+
8
+ import * as fs from "fs";
9
+ import * as path from "path";
10
+ import { USER_CONFIG_DIR } from "./config";
11
+ import { getLogger } from "./logger";
12
+
13
+ const log = getLogger("learn");
14
+
15
+ /* ── Data types ── */
16
+ export interface TaskReview {
17
+ ts: string;
18
+ agent: string;
19
+ goal: string;
20
+ success: boolean;
21
+ durationMs: number;
22
+ toolCalls: string[];
23
+ errorMsg?: string;
24
+ rootCause?: string;
25
+ improvement?: string;
26
+ }
27
+
28
+ export interface ExperienceEntry {
29
+ id: string;
30
+ pattern: string; // What went wrong (key for similarity search)
31
+ solution: string; // What fixed it
32
+ frequency: number; // How often this pattern repeats
33
+ lastSeen: string;
34
+ }
35
+
36
+ /* ── Persistence ── */
37
+ const reviewDir = path.join(USER_CONFIG_DIR, "reviews");
38
+ const expFile = path.join(USER_CONFIG_DIR, "experiences.json");
39
+ const reviewDir_ = reviewDir; // for closure
40
+
41
+ function ensureDir() { if (!fs.existsSync(reviewDir_)) fs.mkdirSync(reviewDir_, { recursive: true }); }
42
+
43
+ /* ═══════════════════════════════════════
44
+ Task Review Recording
45
+ ═══════════════════════════════════════ */
46
+ export function recordReview(review: TaskReview): void {
47
+ ensureDir();
48
+ const file = path.join(reviewDir_, `${review.ts.slice(0, 10)}_${review.agent}.jsonl`);
49
+ const line = JSON.stringify(review);
50
+ fs.appendFileSync(file, line + "\n");
51
+ log.debug("review_recorded", { agent: review.agent, success: review.success });
52
+
53
+ // If failed, also record as experience
54
+ if (!review.success && review.errorMsg) {
55
+ recordExperience(review.errorMsg, review.rootCause || "unknown", review.improvement || "no improvement noted");
56
+ }
57
+ }
58
+
59
+ /* ═══════════════════════════════════════
60
+ Experience Recording (for failure patterns)
61
+ ═══════════════════════════════════════ */
62
+ function loadExperiences(): ExperienceEntry[] {
63
+ try {
64
+ if (fs.existsSync(expFile)) return JSON.parse(fs.readFileSync(expFile, "utf-8"));
65
+ } catch { /* ignore */ }
66
+ return [];
67
+ }
68
+
69
+ function saveExperiences(entries: ExperienceEntry[]): void {
70
+ ensureDir();
71
+ fs.writeFileSync(expFile, JSON.stringify(entries, null, 2), "utf-8");
72
+ }
73
+
74
+ export function recordExperience(errorPattern: string, rootCause: string, solution: string): void {
75
+ const entries = loadExperiences();
76
+ const normalized = errorPattern.toLowerCase().slice(0, 200);
77
+
78
+ // Check for existing similar pattern (simple substring match)
79
+ const existing = entries.find(e => e.pattern.toLowerCase().includes(normalized.slice(0, 50)) || normalized.includes(e.pattern.toLowerCase().slice(0, 50)));
80
+ if (existing) {
81
+ existing.frequency++;
82
+ existing.lastSeen = new Date().toISOString();
83
+ if (solution && solution !== "no improvement noted") existing.solution = solution;
84
+ } else {
85
+ entries.push({
86
+ id: Math.random().toString(36).slice(2, 10),
87
+ pattern: errorPattern.slice(0, 200),
88
+ solution,
89
+ frequency: 1,
90
+ lastSeen: new Date().toISOString(),
91
+ });
92
+ }
93
+
94
+ // Keep top 100 experiences, sorted by frequency
95
+ entries.sort((a, b) => b.frequency - a.frequency);
96
+ if (entries.length > 100) entries.splice(100);
97
+ saveExperiences(entries);
98
+ }
99
+
100
+ /* ═══════════════════════════════════════
101
+ Query experiences
102
+ ═══════════════════════════════════════ */
103
+ export function queryExperiences(problem: string, limit: number = 3): ExperienceEntry[] {
104
+ const entries = loadExperiences();
105
+ const lower = problem.toLowerCase();
106
+ return entries
107
+ .filter(e => {
108
+ const plow = e.pattern.toLowerCase();
109
+ // Simple token overlap scoring
110
+ const tokens = lower.split(/\s+/).filter(t => t.length > 2);
111
+ const matches = tokens.filter(t => plow.includes(t));
112
+ return matches.length >= 2;
113
+ })
114
+ .sort((a, b) => b.frequency - a.frequency)
115
+ .slice(0, limit);
116
+ }
117
+
118
+ /* ═══════════════════════════════════════
119
+ Format experiences for system prompt injection
120
+ ═══════════════════════════════════════ */
121
+ export function formatExperiencesForPrompt(problem: string): string {
122
+ const exps = queryExperiences(problem);
123
+ if (!exps.length) return "";
124
+ const lines = ["## 历史教训(从经验库检索)", "以下是与当前任务相关的过往失败案例,请避免重复:"];
125
+ for (const e of exps) {
126
+ lines.push(`- **模式**: ${e.pattern.slice(0, 120)}`);
127
+ lines.push(` **解决**: ${e.solution.slice(0, 200)} (出现 ${e.frequency} 次)`);
128
+ }
129
+ return lines.join("\n");
130
+ }
131
+
132
+ /* ═══════════════════════════════════════
133
+ Generate a structured review after task completion
134
+ ═══════════════════════════════════════ */
135
+ export function generateReview(
136
+ agent: string, goal: string, success: boolean, durationMs: number,
137
+ toolCalls: string[], errorMsg?: string
138
+ ): TaskReview {
139
+ return {
140
+ ts: new Date().toISOString(),
141
+ agent, goal, success, durationMs, toolCalls,
142
+ errorMsg,
143
+ rootCause: errorMsg ? "auto-detected failure" : undefined,
144
+ improvement: errorMsg ? "review error and adjust approach" : undefined,
145
+ };
146
+ }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * 长文档处理策略 — sliding window + summary chain.
3
+ *
4
+ * When an input exceeds the agent's effective context window,
5
+ * split into overlapping chunks, summarize each, then chain
6
+ * summaries into a final digest.
7
+ *
8
+ * Architecture:
9
+ * Input → Chunk(sliding window) → Per-chunk Summary → Chain → Final Digest
10
+ *
11
+ * All summaries are generated by the calling agent's LLM, so quality
12
+ * depends on the model in use. The chunker is pure text processing
13
+ * and works without any LLM call.
14
+ */
15
+
16
+ import type { BaseAgent } from "./agent";
17
+
18
+ /* ═══════════════════════════════════════
19
+ Chunker — split text into overlapping windows
20
+ ═══════════════════════════════════════ */
21
+ export interface ChunkOptions {
22
+ /** Target chunk size in characters (default 6000) */
23
+ chunkSize?: number;
24
+ /** Overlap between consecutive chunks in characters (default 800) */
25
+ overlap?: number;
26
+ /** Minimum chunk size before we stop splitting (default 500) */
27
+ minChunk?: number;
28
+ }
29
+
30
+ export function chunkText(text: string, opts?: ChunkOptions): string[] {
31
+ const cs = opts?.chunkSize ?? 6000;
32
+ const ol = opts?.overlap ?? 800;
33
+ const min = opts?.minChunk ?? 500;
34
+ const chunks: string[] = [];
35
+
36
+ if (text.length <= cs + min) { chunks.push(text); return chunks; }
37
+
38
+ let start = 0;
39
+ while (start < text.length) {
40
+ let end = start + cs;
41
+ if (end >= text.length) { end = text.length; }
42
+ else {
43
+ // Try to break at paragraph boundary
44
+ const searchEnd = Math.min(end + 400, text.length);
45
+ const paraBreak = text.lastIndexOf("\n\n", searchEnd);
46
+ if (paraBreak > start + min) end = paraBreak;
47
+ else {
48
+ const lineBreak = text.lastIndexOf("\n", searchEnd);
49
+ if (lineBreak > start + min) end = lineBreak;
50
+ else {
51
+ const space = text.lastIndexOf(" ", searchEnd);
52
+ if (space > start + min) end = space;
53
+ }
54
+ }
55
+ }
56
+
57
+ chunks.push(text.slice(start, end).trim());
58
+ if (end >= text.length) break;
59
+ start = end - ol;
60
+ if (start < 0) start = 0;
61
+ }
62
+
63
+ return chunks;
64
+ }
65
+
66
+ /* ═══════════════════════════════════════
67
+ Summary chain — ask agent to summarize chunks then chain
68
+ ═══════════════════════════════════════ */
69
+ export interface SummaryOptions {
70
+ /** Max total chars for the final digest (default 3000) */
71
+ maxDigestChars?: number;
72
+ /** Custom summarization prompt for each chunk */
73
+ chunkPrompt?: string;
74
+ /** Custom chain prompt for combining summaries */
75
+ chainPrompt?: string;
76
+ }
77
+
78
+ const DEFAULT_CHUNK_PROMPT = `Summarize the following text concisely. Keep all key facts, names, numbers, and code snippets. Output the summary directly without preamble. Limit to 300 words.
79
+
80
+ Text:
81
+ {text}`;
82
+
83
+ const DEFAULT_CHAIN_PROMPT = `Combine the following section summaries into a single coherent digest. Preserve all key facts, remove redundancy. Output directly without preamble.
84
+
85
+ {summaries}`;
86
+
87
+ export async function summarizeLongDoc(
88
+ agent: BaseAgent,
89
+ text: string,
90
+ opts?: SummaryOptions
91
+ ): Promise<string> {
92
+ const maxDigest = opts?.maxDigestChars ?? 3000;
93
+ const chunks = chunkText(text);
94
+
95
+ // Single chunk — no summarization needed
96
+ if (chunks.length <= 1) {
97
+ if (text.length <= maxDigest) return text;
98
+ const prompt = (opts?.chunkPrompt || DEFAULT_CHUNK_PROMPT).replace("{text}", text);
99
+ return agent.chatOneshot(prompt, { maxTokens: maxDigest });
100
+ }
101
+
102
+ // Multi-chunk: summarize each, then chain
103
+ const summaries: string[] = [];
104
+ for (let i = 0; i < chunks.length; i++) {
105
+ const prompt = (opts?.chunkPrompt || DEFAULT_CHUNK_PROMPT).replace("{text}", chunks[i]);
106
+ try {
107
+ const s = await agent.chatOneshot(prompt, { maxTokens: 600 });
108
+ summaries.push(s);
109
+ } catch {
110
+ summaries.push(chunks[i].slice(0, 400) + "...");
111
+ }
112
+ }
113
+
114
+ // Chain summaries
115
+ if (summaries.length === 1) return summaries[0].slice(0, maxDigest);
116
+
117
+ const joined = summaries.map((s, i) => `## Section ${i + 1}\n${s}`).join("\n\n");
118
+ if (joined.length <= maxDigest) return joined;
119
+
120
+ const chainPrompt = (opts?.chainPrompt || DEFAULT_CHAIN_PROMPT).replace("{summaries}", joined);
121
+ const final = await agent.chatOneshot(chainPrompt, { maxTokens: maxDigest });
122
+ return final.slice(0, maxDigest);
123
+ }
124
+
125
+ /* ═══════════════════════════════════════
126
+ Structured data parsing helpers
127
+ ═══════════════════════════════════════ */
128
+ export function parseStructuredInput(input: string): {
129
+ hasTable: boolean; hasJSON: boolean; hasCSV: boolean;
130
+ extractedJSON: string | null; extractedTable: string[][] | null;
131
+ } {
132
+ const result = { hasTable: false, hasJSON: false, hasCSV: false, extractedJSON: null as string | null, extractedTable: null as string[][] | null };
133
+
134
+ // Detect JSON
135
+ const jsonMatch = input.match(/\{[\s\S]*\}|\[[\s\S]*\]/);
136
+ if (jsonMatch) {
137
+ try { JSON.parse(jsonMatch[0]); result.hasJSON = true; result.extractedJSON = jsonMatch[0]; }
138
+ catch { /* not valid JSON */ }
139
+ }
140
+
141
+ // Detect markdown table
142
+ const tableMatch = input.match(/\|[\s\S]*?\|/);
143
+ if (tableMatch) {
144
+ result.hasTable = true;
145
+ const lines = input.split("\n").filter(l => l.includes("|") && !l.startsWith("|---") && !l.startsWith("| --"));
146
+ result.extractedTable = lines.map(l => l.split("|").filter(c => c.trim()).map(c => c.trim()));
147
+ }
148
+
149
+ // Detect CSV
150
+ if (input.includes(",") && input.split("\n").filter(l => l.includes(",")).length >= 2) {
151
+ result.hasCSV = true;
152
+ }
153
+
154
+ return result;
155
+ }
@@ -0,0 +1,243 @@
1
+ /**
2
+ * 安全与对齐模块 — Security & Alignment
3
+ *
4
+ * Danger level grading, red-line enforcement, audit trail, human-in-the-loop.
5
+ * All security decisions flow through this module before tool execution.
6
+ */
7
+
8
+ import { getLogger } from "./logger";
9
+
10
+ const log = getLogger("security");
11
+
12
+ /* ── Danger levels ── */
13
+ export enum DangerLevel {
14
+ /** Read-only, no side effects — auto-approved */
15
+ SAFE = 0,
16
+ /** Minor side effects (write single file, git status) — logged */
17
+ LOW = 1,
18
+ /** Significant side effects (overwrite, delete, git push) — notify */
19
+ MEDIUM = 2,
20
+ /** Dangerous (sudo, remote deploy, mass delete) — confirm */
21
+ HIGH = 3,
22
+ /** Red-line — NEVER execute without human-in-the-loop */
23
+ CRITICAL = 4,
24
+ }
25
+
26
+ /* ═══════════════════════════════════════
27
+ Red-line list: operations that are NEVER auto-approved
28
+ ═══════════════════════════════════════ */
29
+ const REDLINE_PATTERNS = [
30
+ /rm\s+-rf/, /format\s+\w:/, /dd\s+if=/,
31
+ />\s*\/dev\/sd/, /mkfs\./, /:(){ :\|:& };:/,
32
+ /sudo\s+rm/, /chmod\s+777\s+\//, /wget.*\|.*sh/,
33
+ /curl.*\|.*bash/, /eval\s+\$/, /exec\s+\$/,
34
+ /subprocess\.call.*rm/, /os\.system.*rm/,
35
+ ];
36
+
37
+ const REDLINE_COMMANDS = [
38
+ "shutdown", "reboot", "init 0", "init 6",
39
+ "del /f /s /q C:\\*", "rd /s /q C:\\",
40
+ ];
41
+
42
+ /* ═══════════════════════════════════════
43
+ Per-tool danger level mapping
44
+ ═══════════════════════════════════════ */
45
+ const TOOL_DANGER_MAP: Record<string, DangerLevel> = {
46
+ read_file: DangerLevel.SAFE,
47
+ list_directory: DangerLevel.SAFE,
48
+ tree: DangerLevel.SAFE,
49
+ file_search: DangerLevel.SAFE,
50
+ code_search: DangerLevel.SAFE,
51
+ grep: DangerLevel.SAFE,
52
+ git_status: DangerLevel.SAFE,
53
+ git_diff: DangerLevel.SAFE,
54
+ git_log: DangerLevel.SAFE,
55
+ system_info: DangerLevel.SAFE,
56
+ system_diagnose: DangerLevel.SAFE,
57
+ list_processes: DangerLevel.SAFE,
58
+ list_installed_apps: DangerLevel.SAFE,
59
+ list_skills: DangerLevel.SAFE,
60
+ recall_facts: DangerLevel.SAFE,
61
+ mcp_list_servers: DangerLevel.SAFE,
62
+
63
+ write_file: DangerLevel.LOW,
64
+ edit_file: DangerLevel.LOW,
65
+ copy_file: DangerLevel.LOW,
66
+ move_file: DangerLevel.LOW,
67
+ http_get: DangerLevel.LOW,
68
+ fetch_page: DangerLevel.LOW,
69
+ web_search: DangerLevel.LOW,
70
+ remember_fact: DangerLevel.LOW,
71
+ use_skill: DangerLevel.LOW,
72
+ task_done: DangerLevel.LOW,
73
+
74
+ delete_file: DangerLevel.MEDIUM,
75
+ git_add: DangerLevel.MEDIUM,
76
+ git_commit: DangerLevel.MEDIUM,
77
+ git_checkout: DangerLevel.MEDIUM,
78
+ http_post: DangerLevel.MEDIUM,
79
+ mcp_add_server: DangerLevel.MEDIUM,
80
+ mcp_remove_server: DangerLevel.MEDIUM,
81
+ launch_app: DangerLevel.MEDIUM,
82
+ open_path: DangerLevel.MEDIUM,
83
+ browser_open: DangerLevel.MEDIUM,
84
+
85
+ run_bash: DangerLevel.HIGH,
86
+ shell_exec: DangerLevel.HIGH,
87
+ kill_process: DangerLevel.HIGH,
88
+ package_manager: DangerLevel.HIGH,
89
+ service_control: DangerLevel.HIGH,
90
+ delegate_to: DangerLevel.HIGH,
91
+ mcp_scaffold_server: DangerLevel.HIGH,
92
+ };
93
+
94
+ /* ═══════════════════════════════════════
95
+ Audit trail entry
96
+ ═══════════════════════════════════════ */
97
+ export interface AuditEntry {
98
+ ts: string;
99
+ agent: string;
100
+ tool: string;
101
+ args: Record<string, any>;
102
+ dangerLevel: DangerLevel;
103
+ approved: boolean;
104
+ result: string;
105
+ durationMs: number;
106
+ traceId: string;
107
+ }
108
+
109
+ /* ═══════════════════════════════════════
110
+ Security context — per-session security state
111
+ ═══════════════════════════════════════ */
112
+ export class SecurityContext {
113
+ public auditLog: AuditEntry[] = [];
114
+ public deniedCount = 0;
115
+ public autoApprovedCount = 0;
116
+ public manualApprovedCount = 0;
117
+ public approvalMode: "auto" | "interactive" | "strict" = "auto";
118
+
119
+ private approvalCallback: ((tool: string, args: Record<string, any>, level: DangerLevel) => Promise<boolean>) | null = null;
120
+
121
+ constructor(opts?: { mode?: "auto" | "interactive" | "strict"; onApprove?: (tool: string, args: Record<string, any>, level: DangerLevel) => Promise<boolean> }) {
122
+ if (opts?.mode) this.approvalMode = opts.mode;
123
+ if (opts?.onApprove) this.approvalCallback = opts.onApprove;
124
+ }
125
+
126
+ /** Get the danger level for a tool. Defaults to SAFE for unknown tools. */
127
+ getDangerLevel(toolName: string): DangerLevel {
128
+ return TOOL_DANGER_MAP[toolName] ?? DangerLevel.SAFE;
129
+ }
130
+
131
+ /** Check if arguments contain red-line patterns (critical danger). */
132
+ checkRedline(toolName: string, args: Record<string, any>): string | null {
133
+ if (toolName !== "run_bash" && toolName !== "shell_exec") return null;
134
+ const cmd = String(args.command || args.cmd || "").toLowerCase();
135
+ for (const pattern of REDLINE_PATTERNS) {
136
+ if (pattern.test(cmd)) return `Red-line pattern detected: ${pattern.source.slice(0, 40)}`;
137
+ }
138
+ for (const forbidden of REDLINE_COMMANDS) {
139
+ if (cmd.includes(forbidden)) return `Red-line command: ${forbidden}`;
140
+ }
141
+ return null;
142
+ }
143
+
144
+ /** Determine whether a tool call is permitted. Returns [approved, reason]. */
145
+ async checkApproval(toolName: string, args: Record<string, any>, agentName: string): Promise<[boolean, string]> {
146
+ const level = this.getDangerLevel(toolName);
147
+
148
+ // Red-line check
149
+ const redline = this.checkRedline(toolName, args);
150
+ if (redline) {
151
+ log.warn("redline_blocked", { agent: agentName, tool: toolName, reason: redline });
152
+ return [false, redline];
153
+ }
154
+
155
+ // Safe — always allow
156
+ if (level === DangerLevel.SAFE) return [true, "safe"];
157
+
158
+ // Strict mode — deny all non-safe
159
+ if (this.approvalMode === "strict") {
160
+ return [false, `Strict mode: tool '${toolName}' (level ${level}) requires manual approval`];
161
+ }
162
+
163
+ // Auto mode — allow LOW, prompt for MEDIUM+, deny CRITICAL
164
+ if (this.approvalMode === "auto") {
165
+ if (level <= DangerLevel.LOW) return [true, "auto-low"];
166
+ if (level === DangerLevel.CRITICAL) return [false, `CRITICAL tool '${toolName}' requires explicit human approval`];
167
+ // MEDIUM/HIGH with auto mode => need callback
168
+ if (this.approvalCallback) {
169
+ const approved = await this.approvalCallback(toolName, args, level);
170
+ return [approved, approved ? "user-approved" : "user-denied"];
171
+ }
172
+ return [true, "auto-med"]; // no callback → auto-allow but log
173
+ }
174
+
175
+ // Interactive mode — prompt for LOW+
176
+ if (this.approvalCallback) {
177
+ const approved = await this.approvalCallback(toolName, args, level);
178
+ return [approved, approved ? "user-approved" : "user-denied"];
179
+ }
180
+ return [true, "no-callback"];
181
+ }
182
+
183
+ /** Record an audit entry. */
184
+ recordAudit(tool: string, agent: string, args: Record<string, any>, dangerLevel: DangerLevel, approved: boolean, resultPreview: string, durationMs: number, traceId: string): void {
185
+ const entry: AuditEntry = {
186
+ ts: new Date().toISOString(),
187
+ agent, tool, args, dangerLevel, approved,
188
+ result: resultPreview.slice(0, 500),
189
+ durationMs, traceId,
190
+ };
191
+ this.auditLog.push(entry);
192
+ if (this.auditLog.length > 5000) this.auditLog.shift();
193
+
194
+ if (approved) {
195
+ if (dangerLevel >= DangerLevel.HIGH) this.manualApprovedCount++;
196
+ else this.autoApprovedCount++;
197
+ } else {
198
+ this.deniedCount++;
199
+ }
200
+
201
+ log.info(dangerLevel >= DangerLevel.HIGH ? "dangerous_tool_executed" : "tool_executed", {
202
+ tool, agent, level: dangerLevel, approved,
203
+ });
204
+ }
205
+
206
+ /** Get summary statistics. */
207
+ getStats() {
208
+ return {
209
+ total: this.auditLog.length,
210
+ denied: this.deniedCount,
211
+ autoApproved: this.autoApprovedCount,
212
+ manualApproved: this.manualApprovedCount,
213
+ byLevel: {
214
+ safe: this.auditLog.filter(e => e.dangerLevel === DangerLevel.SAFE).length,
215
+ low: this.auditLog.filter(e => e.dangerLevel === DangerLevel.LOW).length,
216
+ medium: this.auditLog.filter(e => e.dangerLevel === DangerLevel.MEDIUM).length,
217
+ high: this.auditLog.filter(e => e.dangerLevel === DangerLevel.HIGH).length,
218
+ critical: this.auditLog.filter(e => e.dangerLevel === DangerLevel.CRITICAL).length,
219
+ },
220
+ lastDenied: this.auditLog.filter(e => !e.approved).slice(-5).map(e => `${e.tool}: ${e.result}`),
221
+ };
222
+ }
223
+
224
+ /** Install approval callback for interactive mode. */
225
+ setApprovalCallback(fn: (tool: string, args: Record<string, any>, level: DangerLevel) => Promise<boolean>) {
226
+ this.approvalCallback = fn;
227
+ }
228
+ }
229
+
230
+ /* ── Global security context ── */
231
+ let globalSecurity: SecurityContext | null = null;
232
+
233
+ export function getSecurity(): SecurityContext {
234
+ if (!globalSecurity) globalSecurity = new SecurityContext();
235
+ return globalSecurity;
236
+ }
237
+
238
+ export function resetSecurity(): void {
239
+ globalSecurity = null;
240
+ }
241
+
242
+ /** Red-line patterns for reference (used by tools to self-check). */
243
+ export { REDLINE_PATTERNS, REDLINE_COMMANDS };