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.
- package/dist/core/agent.d.ts +2 -0
- package/dist/core/agent.d.ts.map +1 -1
- package/dist/core/agent.js +33 -6
- package/dist/core/agent.js.map +1 -1
- package/dist/core/arbitrate.d.ts +32 -0
- package/dist/core/arbitrate.d.ts.map +1 -0
- package/dist/core/arbitrate.js +136 -0
- package/dist/core/arbitrate.js.map +1 -0
- package/dist/core/estimate.d.ts +30 -0
- package/dist/core/estimate.d.ts.map +1 -0
- package/dist/core/estimate.js +94 -0
- package/dist/core/estimate.js.map +1 -0
- package/dist/core/filter.d.ts +16 -0
- package/dist/core/filter.d.ts.map +1 -0
- package/dist/core/filter.js +91 -0
- package/dist/core/filter.js.map +1 -0
- package/dist/core/index.d.ts +7 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +13 -2
- package/dist/core/index.js.map +1 -1
- package/dist/core/learn.d.ts +30 -0
- package/dist/core/learn.d.ts.map +1 -0
- package/dist/core/learn.js +156 -0
- package/dist/core/learn.js.map +1 -0
- package/dist/core/longdoc.d.ts +41 -0
- package/dist/core/longdoc.d.ts.map +1 -0
- package/dist/core/longdoc.js +128 -0
- package/dist/core/longdoc.js.map +1 -0
- package/dist/core/security.d.ts +73 -0
- package/dist/core/security.d.ts.map +1 -0
- package/dist/core/security.js +220 -0
- package/dist/core/security.js.map +1 -0
- package/package.json +1 -1
- package/src/core/agent.ts +18 -6
- package/src/core/arbitrate.ts +162 -0
- package/src/core/estimate.ts +104 -0
- package/src/core/filter.ts +103 -0
- package/src/core/index.ts +8 -2
- package/src/core/learn.ts +146 -0
- package/src/core/longdoc.ts +155 -0
- package/src/core/security.ts +243 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longdoc.js","sourceRoot":"","sources":["../../src/core/longdoc.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAa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C,GAAG,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;QACtF,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
export declare enum DangerLevel {
|
|
8
|
+
/** Read-only, no side effects — auto-approved */
|
|
9
|
+
SAFE = 0,
|
|
10
|
+
/** Minor side effects (write single file, git status) — logged */
|
|
11
|
+
LOW = 1,
|
|
12
|
+
/** Significant side effects (overwrite, delete, git push) — notify */
|
|
13
|
+
MEDIUM = 2,
|
|
14
|
+
/** Dangerous (sudo, remote deploy, mass delete) — confirm */
|
|
15
|
+
HIGH = 3,
|
|
16
|
+
/** Red-line — NEVER execute without human-in-the-loop */
|
|
17
|
+
CRITICAL = 4
|
|
18
|
+
}
|
|
19
|
+
declare const REDLINE_PATTERNS: RegExp[];
|
|
20
|
+
declare const REDLINE_COMMANDS: string[];
|
|
21
|
+
export interface AuditEntry {
|
|
22
|
+
ts: string;
|
|
23
|
+
agent: string;
|
|
24
|
+
tool: string;
|
|
25
|
+
args: Record<string, any>;
|
|
26
|
+
dangerLevel: DangerLevel;
|
|
27
|
+
approved: boolean;
|
|
28
|
+
result: string;
|
|
29
|
+
durationMs: number;
|
|
30
|
+
traceId: string;
|
|
31
|
+
}
|
|
32
|
+
export declare class SecurityContext {
|
|
33
|
+
auditLog: AuditEntry[];
|
|
34
|
+
deniedCount: number;
|
|
35
|
+
autoApprovedCount: number;
|
|
36
|
+
manualApprovedCount: number;
|
|
37
|
+
approvalMode: "auto" | "interactive" | "strict";
|
|
38
|
+
private approvalCallback;
|
|
39
|
+
constructor(opts?: {
|
|
40
|
+
mode?: "auto" | "interactive" | "strict";
|
|
41
|
+
onApprove?: (tool: string, args: Record<string, any>, level: DangerLevel) => Promise<boolean>;
|
|
42
|
+
});
|
|
43
|
+
/** Get the danger level for a tool. Defaults to SAFE for unknown tools. */
|
|
44
|
+
getDangerLevel(toolName: string): DangerLevel;
|
|
45
|
+
/** Check if arguments contain red-line patterns (critical danger). */
|
|
46
|
+
checkRedline(toolName: string, args: Record<string, any>): string | null;
|
|
47
|
+
/** Determine whether a tool call is permitted. Returns [approved, reason]. */
|
|
48
|
+
checkApproval(toolName: string, args: Record<string, any>, agentName: string): Promise<[boolean, string]>;
|
|
49
|
+
/** Record an audit entry. */
|
|
50
|
+
recordAudit(tool: string, agent: string, args: Record<string, any>, dangerLevel: DangerLevel, approved: boolean, resultPreview: string, durationMs: number, traceId: string): void;
|
|
51
|
+
/** Get summary statistics. */
|
|
52
|
+
getStats(): {
|
|
53
|
+
total: number;
|
|
54
|
+
denied: number;
|
|
55
|
+
autoApproved: number;
|
|
56
|
+
manualApproved: number;
|
|
57
|
+
byLevel: {
|
|
58
|
+
safe: number;
|
|
59
|
+
low: number;
|
|
60
|
+
medium: number;
|
|
61
|
+
high: number;
|
|
62
|
+
critical: number;
|
|
63
|
+
};
|
|
64
|
+
lastDenied: string[];
|
|
65
|
+
};
|
|
66
|
+
/** Install approval callback for interactive mode. */
|
|
67
|
+
setApprovalCallback(fn: (tool: string, args: Record<string, any>, level: DangerLevel) => Promise<boolean>): void;
|
|
68
|
+
}
|
|
69
|
+
export declare function getSecurity(): SecurityContext;
|
|
70
|
+
export declare function resetSecurity(): void;
|
|
71
|
+
/** Red-line patterns for reference (used by tools to self-check). */
|
|
72
|
+
export { REDLINE_PATTERNS, REDLINE_COMMANDS };
|
|
73
|
+
//# sourceMappingURL=security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AAM,GAAG,WAAW;IAI7C,sEAAsE;IACtE,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,GAAG,IAAI;IAYxE,8EAA8E;IACxE,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAsC/G,6BAA6B;IAC7B,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAsBlL,8BAA8B;IAC9B,QAAQ;;;;;;;;;;;;;;IAiBR,sDAAsD;IACtD,mBAAmB,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,CAAC;CAG1G;AAKD,wBAAgB,WAAW,IAAI,eAAe,CAG7C;AAED,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AAED,qEAAqE;AACrE,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 安全与对齐模块 — Security & Alignment
|
|
4
|
+
*
|
|
5
|
+
* Danger level grading, red-line enforcement, audit trail, human-in-the-loop.
|
|
6
|
+
* All security decisions flow through this module before tool execution.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.REDLINE_COMMANDS = exports.REDLINE_PATTERNS = exports.SecurityContext = exports.DangerLevel = void 0;
|
|
10
|
+
exports.getSecurity = getSecurity;
|
|
11
|
+
exports.resetSecurity = resetSecurity;
|
|
12
|
+
const logger_1 = require("./logger");
|
|
13
|
+
const log = (0, logger_1.getLogger)("security");
|
|
14
|
+
/* ── Danger levels ── */
|
|
15
|
+
var DangerLevel;
|
|
16
|
+
(function (DangerLevel) {
|
|
17
|
+
/** Read-only, no side effects — auto-approved */
|
|
18
|
+
DangerLevel[DangerLevel["SAFE"] = 0] = "SAFE";
|
|
19
|
+
/** Minor side effects (write single file, git status) — logged */
|
|
20
|
+
DangerLevel[DangerLevel["LOW"] = 1] = "LOW";
|
|
21
|
+
/** Significant side effects (overwrite, delete, git push) — notify */
|
|
22
|
+
DangerLevel[DangerLevel["MEDIUM"] = 2] = "MEDIUM";
|
|
23
|
+
/** Dangerous (sudo, remote deploy, mass delete) — confirm */
|
|
24
|
+
DangerLevel[DangerLevel["HIGH"] = 3] = "HIGH";
|
|
25
|
+
/** Red-line — NEVER execute without human-in-the-loop */
|
|
26
|
+
DangerLevel[DangerLevel["CRITICAL"] = 4] = "CRITICAL";
|
|
27
|
+
})(DangerLevel || (exports.DangerLevel = DangerLevel = {}));
|
|
28
|
+
/* ═══════════════════════════════════════
|
|
29
|
+
Red-line list: operations that are NEVER auto-approved
|
|
30
|
+
═══════════════════════════════════════ */
|
|
31
|
+
const REDLINE_PATTERNS = [
|
|
32
|
+
/rm\s+-rf/, /format\s+\w:/, /dd\s+if=/,
|
|
33
|
+
/>\s*\/dev\/sd/, /mkfs\./, /:(){ :\|:& };:/,
|
|
34
|
+
/sudo\s+rm/, /chmod\s+777\s+\//, /wget.*\|.*sh/,
|
|
35
|
+
/curl.*\|.*bash/, /eval\s+\$/, /exec\s+\$/,
|
|
36
|
+
/subprocess\.call.*rm/, /os\.system.*rm/,
|
|
37
|
+
];
|
|
38
|
+
exports.REDLINE_PATTERNS = REDLINE_PATTERNS;
|
|
39
|
+
const REDLINE_COMMANDS = [
|
|
40
|
+
"shutdown", "reboot", "init 0", "init 6",
|
|
41
|
+
"del /f /s /q C:\\*", "rd /s /q C:\\",
|
|
42
|
+
];
|
|
43
|
+
exports.REDLINE_COMMANDS = REDLINE_COMMANDS;
|
|
44
|
+
/* ═══════════════════════════════════════
|
|
45
|
+
Per-tool danger level mapping
|
|
46
|
+
═══════════════════════════════════════ */
|
|
47
|
+
const TOOL_DANGER_MAP = {
|
|
48
|
+
read_file: DangerLevel.SAFE,
|
|
49
|
+
list_directory: DangerLevel.SAFE,
|
|
50
|
+
tree: DangerLevel.SAFE,
|
|
51
|
+
file_search: DangerLevel.SAFE,
|
|
52
|
+
code_search: DangerLevel.SAFE,
|
|
53
|
+
grep: DangerLevel.SAFE,
|
|
54
|
+
git_status: DangerLevel.SAFE,
|
|
55
|
+
git_diff: DangerLevel.SAFE,
|
|
56
|
+
git_log: DangerLevel.SAFE,
|
|
57
|
+
system_info: DangerLevel.SAFE,
|
|
58
|
+
system_diagnose: DangerLevel.SAFE,
|
|
59
|
+
list_processes: DangerLevel.SAFE,
|
|
60
|
+
list_installed_apps: DangerLevel.SAFE,
|
|
61
|
+
list_skills: DangerLevel.SAFE,
|
|
62
|
+
recall_facts: DangerLevel.SAFE,
|
|
63
|
+
mcp_list_servers: DangerLevel.SAFE,
|
|
64
|
+
write_file: DangerLevel.LOW,
|
|
65
|
+
edit_file: DangerLevel.LOW,
|
|
66
|
+
copy_file: DangerLevel.LOW,
|
|
67
|
+
move_file: DangerLevel.LOW,
|
|
68
|
+
http_get: DangerLevel.LOW,
|
|
69
|
+
fetch_page: DangerLevel.LOW,
|
|
70
|
+
web_search: DangerLevel.LOW,
|
|
71
|
+
remember_fact: DangerLevel.LOW,
|
|
72
|
+
use_skill: DangerLevel.LOW,
|
|
73
|
+
task_done: DangerLevel.LOW,
|
|
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
|
+
run_bash: DangerLevel.HIGH,
|
|
85
|
+
shell_exec: DangerLevel.HIGH,
|
|
86
|
+
kill_process: DangerLevel.HIGH,
|
|
87
|
+
package_manager: DangerLevel.HIGH,
|
|
88
|
+
service_control: DangerLevel.HIGH,
|
|
89
|
+
delegate_to: DangerLevel.HIGH,
|
|
90
|
+
mcp_scaffold_server: DangerLevel.HIGH,
|
|
91
|
+
};
|
|
92
|
+
/* ═══════════════════════════════════════
|
|
93
|
+
Security context — per-session security state
|
|
94
|
+
═══════════════════════════════════════ */
|
|
95
|
+
class SecurityContext {
|
|
96
|
+
constructor(opts) {
|
|
97
|
+
this.auditLog = [];
|
|
98
|
+
this.deniedCount = 0;
|
|
99
|
+
this.autoApprovedCount = 0;
|
|
100
|
+
this.manualApprovedCount = 0;
|
|
101
|
+
this.approvalMode = "auto";
|
|
102
|
+
this.approvalCallback = null;
|
|
103
|
+
if (opts?.mode)
|
|
104
|
+
this.approvalMode = opts.mode;
|
|
105
|
+
if (opts?.onApprove)
|
|
106
|
+
this.approvalCallback = opts.onApprove;
|
|
107
|
+
}
|
|
108
|
+
/** Get the danger level for a tool. Defaults to SAFE for unknown tools. */
|
|
109
|
+
getDangerLevel(toolName) {
|
|
110
|
+
return TOOL_DANGER_MAP[toolName] ?? DangerLevel.SAFE;
|
|
111
|
+
}
|
|
112
|
+
/** Check if arguments contain red-line patterns (critical danger). */
|
|
113
|
+
checkRedline(toolName, args) {
|
|
114
|
+
if (toolName !== "run_bash" && toolName !== "shell_exec")
|
|
115
|
+
return null;
|
|
116
|
+
const cmd = String(args.command || args.cmd || "").toLowerCase();
|
|
117
|
+
for (const pattern of REDLINE_PATTERNS) {
|
|
118
|
+
if (pattern.test(cmd))
|
|
119
|
+
return `Red-line pattern detected: ${pattern.source.slice(0, 40)}`;
|
|
120
|
+
}
|
|
121
|
+
for (const forbidden of REDLINE_COMMANDS) {
|
|
122
|
+
if (cmd.includes(forbidden))
|
|
123
|
+
return `Red-line command: ${forbidden}`;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
/** Determine whether a tool call is permitted. Returns [approved, reason]. */
|
|
128
|
+
async checkApproval(toolName, args, agentName) {
|
|
129
|
+
const level = this.getDangerLevel(toolName);
|
|
130
|
+
// Red-line check
|
|
131
|
+
const redline = this.checkRedline(toolName, args);
|
|
132
|
+
if (redline) {
|
|
133
|
+
log.warn("redline_blocked", { agent: agentName, tool: toolName, reason: redline });
|
|
134
|
+
return [false, redline];
|
|
135
|
+
}
|
|
136
|
+
// Safe — always allow
|
|
137
|
+
if (level === DangerLevel.SAFE)
|
|
138
|
+
return [true, "safe"];
|
|
139
|
+
// Strict mode — deny all non-safe
|
|
140
|
+
if (this.approvalMode === "strict") {
|
|
141
|
+
return [false, `Strict mode: tool '${toolName}' (level ${level}) requires manual approval`];
|
|
142
|
+
}
|
|
143
|
+
// Auto mode — allow LOW, prompt for MEDIUM+, deny CRITICAL
|
|
144
|
+
if (this.approvalMode === "auto") {
|
|
145
|
+
if (level <= DangerLevel.LOW)
|
|
146
|
+
return [true, "auto-low"];
|
|
147
|
+
if (level === DangerLevel.CRITICAL)
|
|
148
|
+
return [false, `CRITICAL tool '${toolName}' requires explicit human approval`];
|
|
149
|
+
// MEDIUM/HIGH with auto mode => need callback
|
|
150
|
+
if (this.approvalCallback) {
|
|
151
|
+
const approved = await this.approvalCallback(toolName, args, level);
|
|
152
|
+
return [approved, approved ? "user-approved" : "user-denied"];
|
|
153
|
+
}
|
|
154
|
+
return [true, "auto-med"]; // no callback → auto-allow but log
|
|
155
|
+
}
|
|
156
|
+
// Interactive mode — prompt for LOW+
|
|
157
|
+
if (this.approvalCallback) {
|
|
158
|
+
const approved = await this.approvalCallback(toolName, args, level);
|
|
159
|
+
return [approved, approved ? "user-approved" : "user-denied"];
|
|
160
|
+
}
|
|
161
|
+
return [true, "no-callback"];
|
|
162
|
+
}
|
|
163
|
+
/** Record an audit entry. */
|
|
164
|
+
recordAudit(tool, agent, args, dangerLevel, approved, resultPreview, durationMs, traceId) {
|
|
165
|
+
const entry = {
|
|
166
|
+
ts: new Date().toISOString(),
|
|
167
|
+
agent, tool, args, dangerLevel, approved,
|
|
168
|
+
result: resultPreview.slice(0, 500),
|
|
169
|
+
durationMs, traceId,
|
|
170
|
+
};
|
|
171
|
+
this.auditLog.push(entry);
|
|
172
|
+
if (this.auditLog.length > 5000)
|
|
173
|
+
this.auditLog.shift();
|
|
174
|
+
if (approved) {
|
|
175
|
+
if (dangerLevel >= DangerLevel.HIGH)
|
|
176
|
+
this.manualApprovedCount++;
|
|
177
|
+
else
|
|
178
|
+
this.autoApprovedCount++;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
this.deniedCount++;
|
|
182
|
+
}
|
|
183
|
+
log.info(dangerLevel >= DangerLevel.HIGH ? "dangerous_tool_executed" : "tool_executed", {
|
|
184
|
+
tool, agent, level: dangerLevel, approved,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/** Get summary statistics. */
|
|
188
|
+
getStats() {
|
|
189
|
+
return {
|
|
190
|
+
total: this.auditLog.length,
|
|
191
|
+
denied: this.deniedCount,
|
|
192
|
+
autoApproved: this.autoApprovedCount,
|
|
193
|
+
manualApproved: this.manualApprovedCount,
|
|
194
|
+
byLevel: {
|
|
195
|
+
safe: this.auditLog.filter(e => e.dangerLevel === DangerLevel.SAFE).length,
|
|
196
|
+
low: this.auditLog.filter(e => e.dangerLevel === DangerLevel.LOW).length,
|
|
197
|
+
medium: this.auditLog.filter(e => e.dangerLevel === DangerLevel.MEDIUM).length,
|
|
198
|
+
high: this.auditLog.filter(e => e.dangerLevel === DangerLevel.HIGH).length,
|
|
199
|
+
critical: this.auditLog.filter(e => e.dangerLevel === DangerLevel.CRITICAL).length,
|
|
200
|
+
},
|
|
201
|
+
lastDenied: this.auditLog.filter(e => !e.approved).slice(-5).map(e => `${e.tool}: ${e.result}`),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** Install approval callback for interactive mode. */
|
|
205
|
+
setApprovalCallback(fn) {
|
|
206
|
+
this.approvalCallback = fn;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.SecurityContext = SecurityContext;
|
|
210
|
+
/* ── Global security context ── */
|
|
211
|
+
let globalSecurity = null;
|
|
212
|
+
function getSecurity() {
|
|
213
|
+
if (!globalSecurity)
|
|
214
|
+
globalSecurity = new SecurityContext();
|
|
215
|
+
return globalSecurity;
|
|
216
|
+
}
|
|
217
|
+
function resetSecurity() {
|
|
218
|
+
globalSecurity = null;
|
|
219
|
+
}
|
|
220
|
+
//# sourceMappingURL=security.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security.js","sourceRoot":"","sources":["../../src/core/security.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAmOH,kCAGC;AAED,sCAEC;AAxOD,qCAAqC;AAErC,MAAM,GAAG,GAAG,IAAA,kBAAS,EAAC,UAAU,CAAC,CAAC;AAElC,yBAAyB;AACzB,IAAY,WAWX;AAXD,WAAY,WAAW;IACrB,iDAAiD;IACjD,6CAAQ,CAAA;IACR,kEAAkE;IAClE,2CAAO,CAAA;IACP,sEAAsE;IACtE,iDAAU,CAAA;IACV,6DAA6D;IAC7D,6CAAQ,CAAA;IACR,yDAAyD;IACzD,qDAAY,CAAA;AACd,CAAC,EAXW,WAAW,2BAAX,WAAW,QAWtB;AAED;;6CAE6C;AAC7C,MAAM,gBAAgB,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cAAc,GAAG,IAAI,CAAC;AACxB,CAAC"}
|
package/package.json
CHANGED
package/src/core/agent.ts
CHANGED
|
@@ -222,9 +222,11 @@ export class BaseAgent {
|
|
|
222
222
|
protected injectBehaviorRules(prompt: string): string {
|
|
223
223
|
const lang = (this.config as any).llm?.language || 'zh';
|
|
224
224
|
if (lang === 'en') {
|
|
225
|
-
return prompt +
|
|
225
|
+
return prompt +
|
|
226
|
+
`\n\n## Thinking Protocol\nBefore acting, briefly weigh: (1) **What** is the actual need? (2) **How** sure am I? If <80%, flag with [uncertain] and ask.\nIf stuck, admit it — propose a partial answer or ask the user. Never fabricate.\n\n## Behavior\n- Act, don't narrate. No "I will..." before tool calls.\n- Stay in scope. Do what's asked, then stop.\n- Batch independent tool calls in one response.\n- Verify writes: read back, report verified state.\n- Call list_skills when the task needs specialized capabilities.`;
|
|
226
227
|
}
|
|
227
|
-
return prompt +
|
|
228
|
+
return prompt +
|
|
229
|
+
`\n\n## 思考协议\n行动前快速判断:(1) 用户真实需求是什么?(2) 我有多大把握?低于80%标注 [不确定] 并主动询问。\n卡住时承认,给出部分答案或请求用户指导。绝不编造。\n\n## 行为守则\n- 直接行动,不预告。不说「我将要...」,直接调用工具\n- 不擅自扩大范围。用户要什么做什么,核心完成即止\n- 独立的工具调用一次发出,并行执行\n- 写入后回读验证,汇报已验证状态而非仅尝试\n- 任务涉及专业能力时(PPT/Excel/PDF/网页设计/代码审查等),先调 list_skills 查看可用技能,再用 use_skill 激活`;
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
protected injectProgrammingWisdom(prompt: string): string {
|
|
@@ -729,7 +731,9 @@ export class BaseAgent {
|
|
|
729
731
|
try {
|
|
730
732
|
if (onStatus) onStatus('thinking...');
|
|
731
733
|
const response = await this.llmLoop({ onStatus });
|
|
732
|
-
|
|
734
|
+
let content = response?.content || '(no response)';
|
|
735
|
+
// Apply output filter for sensitive info
|
|
736
|
+
try { const { filterOutput } = require('./filter'); const fr = filterOutput(content); if (fr.redacted) content = fr.clean; } catch {}
|
|
733
737
|
this.memory.addMessage('assistant', content, {
|
|
734
738
|
toolCalls: response?.toolCalls || [],
|
|
735
739
|
reasoningContent: response?.reasoningContent,
|
|
@@ -1401,12 +1405,20 @@ export class BaseAgent {
|
|
|
1401
1405
|
}
|
|
1402
1406
|
}
|
|
1403
1407
|
|
|
1408
|
+
private _security: any = null;
|
|
1409
|
+
get security(): any { if (!this._security) { try { const { getSecurity } = require('./security'); this._security = getSecurity(); } catch { this._security = {}; } } return this._security; }
|
|
1410
|
+
|
|
1404
1411
|
protected async checkToolApproval(toolName: string, toolArgs: Record<string, any>): Promise<boolean> {
|
|
1412
|
+
try {
|
|
1413
|
+
const sec = this.security;
|
|
1414
|
+
if (sec?.checkApproval) {
|
|
1415
|
+
const [approved, reason] = await sec.checkApproval(toolName, toolArgs, this.name);
|
|
1416
|
+
if (!approved) log.warn('tool_blocked', { tool: toolName, agent: this.name, reason });
|
|
1417
|
+
return approved;
|
|
1418
|
+
}
|
|
1419
|
+
} catch { /* fall through */ }
|
|
1405
1420
|
const mode = (this.config as any).cli?.approvalMode || 'auto';
|
|
1406
1421
|
if (mode === 'strict') return false;
|
|
1407
|
-
if (mode === 'interactive' && this.approvalCallback) {
|
|
1408
|
-
return this.approvalCallback(toolName, toolArgs);
|
|
1409
|
-
}
|
|
1410
1422
|
return true;
|
|
1411
1423
|
}
|
|
1412
1424
|
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 多Agent冲突仲裁 — majority voting, quality scoring, tie-breaking.
|
|
3
|
+
*
|
|
4
|
+
* When multiple agents produce conflicting outputs on the same task
|
|
5
|
+
* (or when a reviewer disagrees with an executor), this module
|
|
6
|
+
* provides structured conflict resolution.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { TaskExecutionResult } from "./factory";
|
|
10
|
+
|
|
11
|
+
/* ═══════════════════════════════════════
|
|
12
|
+
Conflict detection
|
|
13
|
+
═══════════════════════════════════════ */
|
|
14
|
+
export interface Conflict {
|
|
15
|
+
taskId: string;
|
|
16
|
+
results: TaskExecutionResult[];
|
|
17
|
+
description: string;
|
|
18
|
+
severity: "low" | "medium" | "high";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Detect if two results conflict based on success status and content overlap. */
|
|
22
|
+
export function detectConflicts(results: TaskExecutionResult[]): Conflict[] {
|
|
23
|
+
const byTask = new Map<string, TaskExecutionResult[]>();
|
|
24
|
+
for (const r of results) { const id = r.id; if (!byTask.has(id)) byTask.set(id, []); byTask.get(id)!.push(r); }
|
|
25
|
+
|
|
26
|
+
const conflicts: Conflict[] = [];
|
|
27
|
+
for (const [id, items] of byTask) {
|
|
28
|
+
if (items.length < 2) continue;
|
|
29
|
+
|
|
30
|
+
const successes = items.filter(r => r.success);
|
|
31
|
+
const failures = items.filter(r => !r.success);
|
|
32
|
+
|
|
33
|
+
// All succeeded — check content divergence
|
|
34
|
+
if (successes.length >= 2) {
|
|
35
|
+
const contents = successes.map(r => (r.content || "").toLowerCase());
|
|
36
|
+
const similarity = pairwiseSimilarity(contents);
|
|
37
|
+
if (similarity < 0.3) {
|
|
38
|
+
conflicts.push({ taskId: id, results: successes, description: "Multiple agents produced divergent successful outputs", severity: "medium" });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Mix of success and failure
|
|
43
|
+
if (successes.length > 0 && failures.length > 0) {
|
|
44
|
+
conflicts.push({ taskId: id, results: items, description: `${successes.length} succeeded, ${failures.length} failed — need tiebreaker`, severity: "medium" });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// All failed
|
|
48
|
+
if (failures.length >= 2 && successes.length === 0) {
|
|
49
|
+
conflicts.push({ taskId: id, results: failures, description: "All agents failed on this task", severity: "high" });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return conflicts;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* ═══════════════════════════════════════
|
|
57
|
+
Content similarity (n-gram Jaccard)
|
|
58
|
+
═══════════════════════════════════════ */
|
|
59
|
+
function pairwiseSimilarity(texts: string[]): number {
|
|
60
|
+
if (texts.length < 2) return 1.0;
|
|
61
|
+
let total = 0;
|
|
62
|
+
let count = 0;
|
|
63
|
+
for (let i = 0; i < texts.length; i++) {
|
|
64
|
+
for (let j = i + 1; j < texts.length; j++) {
|
|
65
|
+
total += ngramJaccard(texts[i], texts[j], 3);
|
|
66
|
+
count++;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return count === 0 ? 0 : total / count;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function ngramJaccard(a: string, b: string, n: number): number {
|
|
73
|
+
const as = ngrams(a, n), bs = ngrams(b, n);
|
|
74
|
+
if (as.size === 0 && bs.size === 0) return 1;
|
|
75
|
+
let intersection = 0;
|
|
76
|
+
for (const g of as) { if (bs.has(g)) intersection++; }
|
|
77
|
+
const union = as.size + bs.size - intersection;
|
|
78
|
+
return union === 0 ? 0 : intersection / union;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function ngrams(s: string, n: number): Set<string> {
|
|
82
|
+
const out = new Set<string>();
|
|
83
|
+
for (let i = 0; i <= s.length - n; i++) out.add(s.slice(i, i + n));
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* ═══════════════════════════════════════
|
|
88
|
+
Majority voting / arbitration
|
|
89
|
+
═══════════════════════════════════════ */
|
|
90
|
+
export interface ArbitrationResult {
|
|
91
|
+
winner: TaskExecutionResult;
|
|
92
|
+
method: "unanimous" | "majority" | "tiebreaker" | "single";
|
|
93
|
+
confidence: number; // 0-1
|
|
94
|
+
reasoning: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Pick the best result from conflicting ones via majority vote. */
|
|
98
|
+
export function arbitrate(results: TaskExecutionResult[]): ArbitrationResult {
|
|
99
|
+
if (results.length === 0) throw new Error("No results to arbitrate");
|
|
100
|
+
if (results.length === 1) return { winner: results[0], method: "single", confidence: 0.8, reasoning: "Only one result available" };
|
|
101
|
+
|
|
102
|
+
const success = results.filter(r => r.success);
|
|
103
|
+
const fail = results.filter(r => !r.success);
|
|
104
|
+
|
|
105
|
+
// All agree (success)
|
|
106
|
+
if (success.length === results.length) {
|
|
107
|
+
const longest = success.reduce((a, b) => (b.content || "").length > (a.content || "").length ? b : a);
|
|
108
|
+
return { winner: longest, method: "unanimous", confidence: 0.95, reasoning: `${results.length}/${results.length} agents agreed` };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Majority success
|
|
112
|
+
if (success.length > fail.length) {
|
|
113
|
+
// Pick the longest successful content (most detailed)
|
|
114
|
+
const best = success.reduce((a, b) => (b.content || "").length > (a.content || "").length ? b : a);
|
|
115
|
+
return { winner: best, method: "majority", confidence: success.length / results.length, reasoning: `${success.length}/${results.length} succeeded, selected most detailed` };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Majority failure — pick the "closest to success" (longest content)
|
|
119
|
+
if (fail.length > success.length) {
|
|
120
|
+
const best = fail.reduce((a, b) => (b.content || "").length > (a.content || "").length ? b : a);
|
|
121
|
+
return { winner: best, method: "majority", confidence: 0.3, reasoning: `Majority failed (${fail.length}/${results.length}), best-effort from partial output` };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Tie — prefer success, or longest content
|
|
125
|
+
const tie = success.length > 0 ? success[0] : fail[0];
|
|
126
|
+
return { winner: tie, method: "tiebreaker", confidence: 0.5, reasoning: `Tie — selected ${tie.success ? "success" : "longest"} result` };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* ═══════════════════════════════════════
|
|
130
|
+
Quality scoring for individual results
|
|
131
|
+
═══════════════════════════════════════ */
|
|
132
|
+
export interface QualityScore {
|
|
133
|
+
score: number; // 0-100
|
|
134
|
+
completeness: number; // how much of the task was addressed
|
|
135
|
+
richness: number; // detail level of the output
|
|
136
|
+
correctness: number; // did it match expectations (requires ground truth)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function scoreQuality(result: TaskExecutionResult): QualityScore {
|
|
140
|
+
const content = result.content || "";
|
|
141
|
+
|
|
142
|
+
// Completeness: length is a weak proxy but useful
|
|
143
|
+
const completeness = content.length > 500 ? 80 : content.length > 100 ? 50 : content.length > 0 ? 20 : 0;
|
|
144
|
+
|
|
145
|
+
// Richness: code blocks, structured output, bullet points
|
|
146
|
+
let richness = 50;
|
|
147
|
+
if (/```/.test(content)) richness += 20;
|
|
148
|
+
if (/\|.*\|.*\|/.test(content)) richness += 15; // tables
|
|
149
|
+
if (/^[-*] /.test(content)) richness += 10; // bullets
|
|
150
|
+
if (/\d+\./.test(content)) richness += 10; // numbered lists
|
|
151
|
+
richness = Math.min(100, richness);
|
|
152
|
+
|
|
153
|
+
// Correctness: basic sanity checks
|
|
154
|
+
let correctness = 70;
|
|
155
|
+
if (content.includes("Error") || content.includes("error")) correctness -= 20;
|
|
156
|
+
if (content.includes("[REDACTED]")) correctness -= 10;
|
|
157
|
+
if (content.includes("truncated")) correctness -= 15;
|
|
158
|
+
correctness = Math.max(0, correctness);
|
|
159
|
+
|
|
160
|
+
const score = Math.round((completeness * 0.3 + richness * 0.3 + correctness * 0.4));
|
|
161
|
+
return { score, completeness, richness, correctness };
|
|
162
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 资源估算模块 — Token & time budget estimation for task planning.
|
|
3
|
+
*
|
|
4
|
+
* Helps Snow and other planning agents estimate the cost of
|
|
5
|
+
* proposed sub-tasks before committing to execution.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Task } from "./agent";
|
|
9
|
+
|
|
10
|
+
/* ═══════════════════════════════════════
|
|
11
|
+
Token estimation
|
|
12
|
+
═══════════════════════════════════════ */
|
|
13
|
+
const CJK_REGEX = /[一-鿿-ゟ가-㐀-䶿]/g;
|
|
14
|
+
|
|
15
|
+
/** Estimate tokens for a given text (CJK ~2 each, ASCII ~4 chars each). */
|
|
16
|
+
export function estimateTokens(text: string): number {
|
|
17
|
+
const cjk = (text.match(CJK_REGEX) || []).length;
|
|
18
|
+
return cjk * 2 + Math.ceil((text.length - cjk) / 4);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* ═══════════════════════════════════════
|
|
22
|
+
Per-task-type cost estimates
|
|
23
|
+
═══════════════════════════════════════ */
|
|
24
|
+
const TASK_TYPE_PATTERNS: Array<[RegExp, number, number]> = [
|
|
25
|
+
// [pattern, estimated tokens, estimated tools]
|
|
26
|
+
[/read|read_file|grep|search|查|搜索|list/i, 2000, 2],
|
|
27
|
+
[/write|write_file|生成|写|create|implement/i, 4000, 5],
|
|
28
|
+
[/edit|edit_file|改|修改|fix|修复/i, 3000, 3],
|
|
29
|
+
[/delete|delete_file|删|rm/i, 1500, 2],
|
|
30
|
+
[/deploy|部署|publish|发布|release/i, 8000, 8],
|
|
31
|
+
[/review|审查|audit|审计|scan|扫描/i, 5000, 4],
|
|
32
|
+
[/test|测试|run_test|coverage/i, 3000, 3],
|
|
33
|
+
[/research|研究|调研|analyze|分析/i, 6000, 4],
|
|
34
|
+
[/orchestrate|编排|multi-step|多步/i, 12000, 10],
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** Estimate cost for a single task description. */
|
|
38
|
+
export function estimateTaskCost(description: string): { tokens: number; tools: number; timeSeconds: number } {
|
|
39
|
+
let tokens = 2000; // base
|
|
40
|
+
let tools = 2; // base
|
|
41
|
+
for (const [pattern, t, tc] of TASK_TYPE_PATTERNS) {
|
|
42
|
+
if (pattern.test(description)) { tokens = Math.max(tokens, t); tools = Math.max(tools, tc); }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Time estimate: ~0.5s per tool call + 2s per 1k tokens
|
|
46
|
+
const timeSeconds = (tokens / 1000) * 2 + tools * 0.5 + 2;
|
|
47
|
+
return { tokens, tools, timeSeconds };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/* ═══════════════════════════════════════
|
|
51
|
+
Task plan cost summary
|
|
52
|
+
═══════════════════════════════════════ */
|
|
53
|
+
export interface PlanEstimate {
|
|
54
|
+
totalTokens: number;
|
|
55
|
+
totalTools: number;
|
|
56
|
+
totalTimeSeconds: number;
|
|
57
|
+
perTask: Array<{ id: string; tokens: number; tools: number; time: number }>;
|
|
58
|
+
warnings: string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function estimateTaskPlan(tasks: Task[]): PlanEstimate {
|
|
62
|
+
const perTask: PlanEstimate["perTask"] = [];
|
|
63
|
+
let totalTokens = 500; // system prompt overhead
|
|
64
|
+
let totalTools = 0;
|
|
65
|
+
let totalTime = 5; // init overhead
|
|
66
|
+
const warnings: string[] = [];
|
|
67
|
+
|
|
68
|
+
for (const t of tasks) {
|
|
69
|
+
const est = estimateTaskCost(t.description);
|
|
70
|
+
perTask.push({ id: t.id, tokens: est.tokens, tools: est.tools, time: est.timeSeconds });
|
|
71
|
+
totalTokens += est.tokens;
|
|
72
|
+
totalTools += est.tools;
|
|
73
|
+
totalTime += est.timeSeconds;
|
|
74
|
+
|
|
75
|
+
if (est.timeSeconds > 60) warnings.push(`Task ${t.id} may take >${Math.round(est.timeSeconds)}s`);
|
|
76
|
+
if (est.tools > 10) warnings.push(`Task ${t.id} uses many tool calls (${est.tools})`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (totalTokens > 64000) warnings.push(`Total token estimate (${totalTokens}) exceeds typical context window`);
|
|
80
|
+
if (totalTime > 120) warnings.push(`Estimated total time (${Math.round(totalTime)}s) is significant`);
|
|
81
|
+
if (tasks.length > 6) warnings.push(`Large number of sub-tasks (${tasks.length}) — consider merging simpler ones`);
|
|
82
|
+
|
|
83
|
+
return { totalTokens, totalTools, totalTimeSeconds: Math.round(totalTime), perTask, warnings };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/* ═══════════════════════════════════════
|
|
87
|
+
Format estimate for display
|
|
88
|
+
═══════════════════════════════════════ */
|
|
89
|
+
export function formatPlanEstimate(est: PlanEstimate): string {
|
|
90
|
+
const lines: string[] = [
|
|
91
|
+
`## Plan Estimate`,
|
|
92
|
+
`| Task | Tokens | Tools | Time |`,
|
|
93
|
+
`|------|--------|-------|------|`,
|
|
94
|
+
...est.perTask.map(t => `| ${t.id} | ${t.tokens} | ${t.tools} | ${t.time.toFixed(0)}s |`),
|
|
95
|
+
`| **Total** | **${est.totalTokens}** | **${est.totalTools}** | **${est.totalTimeSeconds}s** |`,
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
if (est.warnings.length > 0) {
|
|
99
|
+
lines.push("", "### Warnings");
|
|
100
|
+
for (const w of est.warnings) lines.push(`- ⚠ ${w}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return lines.join("\n");
|
|
104
|
+
}
|