@kenhkl/agent-gate 0.8.2 → 0.9.1
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/trigger_rules.yaml +585 -0
- package/dist/shared/storage/db.d.ts +26 -9
- package/dist/shared/storage/db.js +281 -47
- package/dist/v1/cli.js +419 -64
- package/dist/v1/server.js +326 -64
- package/dist/v3/cli.js +545 -236
- package/package.json +1 -1
|
@@ -41,6 +41,18 @@ var init_cjs_shims = __esm({
|
|
|
41
41
|
function env(name, dflt = "") {
|
|
42
42
|
return (process.env[name] ?? dflt).trim();
|
|
43
43
|
}
|
|
44
|
+
function envInt(name, dflt) {
|
|
45
|
+
const raw = env(name);
|
|
46
|
+
if (!raw) return dflt;
|
|
47
|
+
const n = parseInt(raw, 10);
|
|
48
|
+
return Number.isFinite(n) ? n : dflt;
|
|
49
|
+
}
|
|
50
|
+
function maxRecordBytes() {
|
|
51
|
+
return envInt("AGENT_GATE_MAX_RECORD_BYTES", 1024 * 1024);
|
|
52
|
+
}
|
|
53
|
+
function diskDegradePercent() {
|
|
54
|
+
return envInt("AGENT_GATE_DISK_DEGRADE_PERCENT", 2);
|
|
55
|
+
}
|
|
44
56
|
function hardenFilesEnabled() {
|
|
45
57
|
return TRUTHY.includes(env("AGENT_GATE_HARDEN_FILES", "1").toLowerCase());
|
|
46
58
|
}
|
|
@@ -57,6 +69,66 @@ var init_security = __esm({
|
|
|
57
69
|
}
|
|
58
70
|
});
|
|
59
71
|
|
|
72
|
+
// src/shared/storage/record-guard.ts
|
|
73
|
+
function clipText(value, limit = maxRecordBytes()) {
|
|
74
|
+
const s = String(value ?? "");
|
|
75
|
+
if (limit <= 0 || s.length <= limit) return s;
|
|
76
|
+
const head = s.slice(0, limit);
|
|
77
|
+
const digest = (0, import_crypto.createHash)("sha256").update(s).digest("hex").slice(0, 16);
|
|
78
|
+
return `${head}\u2026[truncated ${s.length - limit}B sha256:${digest}]`;
|
|
79
|
+
}
|
|
80
|
+
function diskFreePercent(targetPath) {
|
|
81
|
+
try {
|
|
82
|
+
const st = (0, import_fs.statfsSync)((0, import_path.dirname)(targetPath));
|
|
83
|
+
const total = Number(st.blocks) || 0;
|
|
84
|
+
const avail = Number(st.bavail) || 0;
|
|
85
|
+
if (!total) return 100;
|
|
86
|
+
return avail / total * 100;
|
|
87
|
+
} catch {
|
|
88
|
+
return 100;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function shouldDegradeWrites(targetPath) {
|
|
92
|
+
const degrade = diskDegradePercent();
|
|
93
|
+
if (degrade <= 0) return false;
|
|
94
|
+
return diskFreePercent(targetPath) < degrade;
|
|
95
|
+
}
|
|
96
|
+
function degradeRecord(rec) {
|
|
97
|
+
const command = String(rec.command ?? "");
|
|
98
|
+
const clipped = command.length > DEGRADE_COMMAND_BYTES ? `${command.slice(0, DEGRADE_COMMAND_BYTES)}\u2026[degraded]` : command;
|
|
99
|
+
return {
|
|
100
|
+
...rec,
|
|
101
|
+
command: clipped,
|
|
102
|
+
action_detail: String(rec.action_detail ?? "") > "" ? String(rec.action_detail).slice(0, DEGRADE_COMMAND_BYTES) : "",
|
|
103
|
+
description: "",
|
|
104
|
+
reason: String(rec.reason ?? "").slice(0, 256)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function prepareRecordForStorage(rec, targetPath) {
|
|
108
|
+
let out = {
|
|
109
|
+
...rec,
|
|
110
|
+
command: clipText(rec.command),
|
|
111
|
+
action_detail: clipText(rec.action_detail),
|
|
112
|
+
reason: clipText(rec.reason, 4096),
|
|
113
|
+
description: clipText(rec.description, 4096)
|
|
114
|
+
};
|
|
115
|
+
const degraded = shouldDegradeWrites(targetPath);
|
|
116
|
+
if (degraded) out = degradeRecord(out);
|
|
117
|
+
return { record: out, degraded };
|
|
118
|
+
}
|
|
119
|
+
var import_crypto, import_fs, import_path, DEGRADE_COMMAND_BYTES;
|
|
120
|
+
var init_record_guard = __esm({
|
|
121
|
+
"src/shared/storage/record-guard.ts"() {
|
|
122
|
+
"use strict";
|
|
123
|
+
init_cjs_shims();
|
|
124
|
+
import_crypto = require("crypto");
|
|
125
|
+
import_fs = require("fs");
|
|
126
|
+
import_path = require("path");
|
|
127
|
+
init_security();
|
|
128
|
+
DEGRADE_COMMAND_BYTES = 512;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
60
132
|
// src/shared/storage/json-storage.ts
|
|
61
133
|
var json_storage_exports = {};
|
|
62
134
|
__export(json_storage_exports, {
|
|
@@ -67,17 +139,18 @@ function nowISO() {
|
|
|
67
139
|
}
|
|
68
140
|
function getDefaultDataDir() {
|
|
69
141
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
70
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
71
|
-
return (0,
|
|
142
|
+
const xdgData = process.env.XDG_DATA_HOME || (0, import_path2.join)(home, ".local", "share");
|
|
143
|
+
return (0, import_path2.join)(xdgData, "agent_gate", "storage");
|
|
72
144
|
}
|
|
73
|
-
var
|
|
145
|
+
var import_fs2, import_path2, JsonStorage;
|
|
74
146
|
var init_json_storage = __esm({
|
|
75
147
|
"src/shared/storage/json-storage.ts"() {
|
|
76
148
|
"use strict";
|
|
77
149
|
init_cjs_shims();
|
|
78
|
-
|
|
79
|
-
|
|
150
|
+
import_fs2 = require("fs");
|
|
151
|
+
import_path2 = require("path");
|
|
80
152
|
init_security();
|
|
153
|
+
init_record_guard();
|
|
81
154
|
JsonStorage = class {
|
|
82
155
|
dataDir;
|
|
83
156
|
agentsPath;
|
|
@@ -90,11 +163,11 @@ var init_json_storage = __esm({
|
|
|
90
163
|
sessionLocks = /* @__PURE__ */ new Map();
|
|
91
164
|
constructor(dataDir) {
|
|
92
165
|
this.dataDir = dataDir || process.env.AGENT_GATE_DATA_DIR || getDefaultDataDir();
|
|
93
|
-
this.agentsPath = (0,
|
|
94
|
-
this.sessionsPath = (0,
|
|
95
|
-
this.toolCallsDir = (0,
|
|
96
|
-
if (!(0,
|
|
97
|
-
if (!(0,
|
|
166
|
+
this.agentsPath = (0, import_path2.join)(this.dataDir, "agents.json");
|
|
167
|
+
this.sessionsPath = (0, import_path2.join)(this.dataDir, "sessions.json");
|
|
168
|
+
this.toolCallsDir = (0, import_path2.join)(this.dataDir, "tool_calls");
|
|
169
|
+
if (!(0, import_fs2.existsSync)(this.dataDir)) (0, import_fs2.mkdirSync)(this.dataDir, { recursive: true, mode: 448 });
|
|
170
|
+
if (!(0, import_fs2.existsSync)(this.toolCallsDir)) (0, import_fs2.mkdirSync)(this.toolCallsDir, { recursive: true, mode: 448 });
|
|
98
171
|
this.hardenStoragePermissions();
|
|
99
172
|
this.nextId = this.recoverNextId();
|
|
100
173
|
}
|
|
@@ -106,10 +179,10 @@ var init_json_storage = __esm({
|
|
|
106
179
|
const uid = process.getuid?.();
|
|
107
180
|
const tighten = (p, mode) => {
|
|
108
181
|
try {
|
|
109
|
-
const st = (0,
|
|
182
|
+
const st = (0, import_fs2.statSync)(p);
|
|
110
183
|
if (st.isSymbolicLink()) return;
|
|
111
184
|
if (uid !== void 0 && st.uid !== uid) return;
|
|
112
|
-
if ((st.mode & 511) !== mode) (0,
|
|
185
|
+
if ((st.mode & 511) !== mode) (0, import_fs2.chmodSync)(p, mode);
|
|
113
186
|
} catch {
|
|
114
187
|
}
|
|
115
188
|
};
|
|
@@ -118,9 +191,9 @@ var init_json_storage = __esm({
|
|
|
118
191
|
tighten(this.agentsPath, 384);
|
|
119
192
|
tighten(this.sessionsPath, 384);
|
|
120
193
|
try {
|
|
121
|
-
for (const f of (0,
|
|
194
|
+
for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
|
|
122
195
|
if (f.endsWith(".json") || f.endsWith(".tmp")) {
|
|
123
|
-
tighten((0,
|
|
196
|
+
tighten((0, import_path2.join)(this.toolCallsDir, f), 384);
|
|
124
197
|
}
|
|
125
198
|
}
|
|
126
199
|
} catch {
|
|
@@ -130,9 +203,9 @@ var init_json_storage = __esm({
|
|
|
130
203
|
recoverNextId() {
|
|
131
204
|
let maxId = 0;
|
|
132
205
|
try {
|
|
133
|
-
const files = (0,
|
|
206
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
134
207
|
for (const file of files) {
|
|
135
|
-
const records = this.readJsonFile((0,
|
|
208
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
136
209
|
for (const r of records) {
|
|
137
210
|
if (r.id > maxId) maxId = r.id;
|
|
138
211
|
}
|
|
@@ -143,19 +216,19 @@ var init_json_storage = __esm({
|
|
|
143
216
|
}
|
|
144
217
|
readJsonFile(path, fallback) {
|
|
145
218
|
try {
|
|
146
|
-
if (!(0,
|
|
147
|
-
return JSON.parse((0,
|
|
219
|
+
if (!(0, import_fs2.existsSync)(path)) return fallback;
|
|
220
|
+
return JSON.parse((0, import_fs2.readFileSync)(path, "utf-8"));
|
|
148
221
|
} catch {
|
|
149
222
|
return fallback;
|
|
150
223
|
}
|
|
151
224
|
}
|
|
152
225
|
/** 原子写入:先写 .tmp 再 rename(rename 保留 tmp 的 0600 权限) */
|
|
153
226
|
writeJsonFile(path, data) {
|
|
154
|
-
const dir = (0,
|
|
155
|
-
if (!(0,
|
|
227
|
+
const dir = (0, import_path2.dirname)(path);
|
|
228
|
+
if (!(0, import_fs2.existsSync)(dir)) (0, import_fs2.mkdirSync)(dir, { recursive: true, mode: 448 });
|
|
156
229
|
const tmp = path + ".tmp";
|
|
157
|
-
(0,
|
|
158
|
-
(0,
|
|
230
|
+
(0, import_fs2.writeFileSync)(tmp, JSON.stringify(data, null, 2), { encoding: "utf-8", mode: 384 });
|
|
231
|
+
(0, import_fs2.renameSync)(tmp, path);
|
|
159
232
|
}
|
|
160
233
|
loadAgents() {
|
|
161
234
|
const raw = this.readJsonFile(this.agentsPath, {});
|
|
@@ -173,12 +246,12 @@ var init_json_storage = __esm({
|
|
|
173
246
|
}
|
|
174
247
|
loadToolCalls(sessionId) {
|
|
175
248
|
return this.readJsonFile(
|
|
176
|
-
(0,
|
|
249
|
+
(0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`),
|
|
177
250
|
[]
|
|
178
251
|
);
|
|
179
252
|
}
|
|
180
253
|
saveToolCalls(sessionId, records) {
|
|
181
|
-
this.writeJsonFile((0,
|
|
254
|
+
this.writeJsonFile((0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`), records);
|
|
182
255
|
}
|
|
183
256
|
getOrCreateAgent(agentType, hostname, version = "") {
|
|
184
257
|
const agents = this.loadAgents();
|
|
@@ -223,6 +296,10 @@ var init_json_storage = __esm({
|
|
|
223
296
|
return Math.max(...records.map((r) => r.seq)) + 1;
|
|
224
297
|
}
|
|
225
298
|
insertToolCall(record) {
|
|
299
|
+
record = prepareRecordForStorage(
|
|
300
|
+
record,
|
|
301
|
+
this.dataDir
|
|
302
|
+
).record;
|
|
226
303
|
const sessionId = record.session_id;
|
|
227
304
|
const prev = this.sessionLocks.get(sessionId) ?? Promise.resolve();
|
|
228
305
|
const next = prev.then(() => {
|
|
@@ -260,9 +337,9 @@ var init_json_storage = __esm({
|
|
|
260
337
|
allRecords = records.map((r) => this.enrichRecord(r, sessions, agents));
|
|
261
338
|
} else {
|
|
262
339
|
try {
|
|
263
|
-
const files = (0,
|
|
340
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
264
341
|
for (const file of files) {
|
|
265
|
-
const records = this.readJsonFile((0,
|
|
342
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
266
343
|
allRecords.push(...records.map((r) => this.enrichRecord(r, sessions, agents)));
|
|
267
344
|
}
|
|
268
345
|
} catch {
|
|
@@ -271,9 +348,21 @@ var init_json_storage = __esm({
|
|
|
271
348
|
if (params.agentType) {
|
|
272
349
|
allRecords = allRecords.filter((r) => r.agent_type === params.agentType);
|
|
273
350
|
}
|
|
351
|
+
if (params.agentId) {
|
|
352
|
+
allRecords = allRecords.filter((r) => `${r.agent_type}:${r.hostname}` === params.agentId);
|
|
353
|
+
}
|
|
274
354
|
if (params.decision) {
|
|
275
355
|
allRecords = allRecords.filter((r) => r.decision === params.decision);
|
|
276
356
|
}
|
|
357
|
+
if (params.riskLevel) {
|
|
358
|
+
allRecords = allRecords.filter((r) => r.risk_level === params.riskLevel);
|
|
359
|
+
}
|
|
360
|
+
if (params.fromTime) {
|
|
361
|
+
allRecords = allRecords.filter((r) => r.called_at >= params.fromTime);
|
|
362
|
+
}
|
|
363
|
+
if (params.toTime) {
|
|
364
|
+
allRecords = allRecords.filter((r) => r.called_at <= params.toTime);
|
|
365
|
+
}
|
|
277
366
|
allRecords.sort((a, b) => b.called_at.localeCompare(a.called_at));
|
|
278
367
|
const offset = params.offset || 0;
|
|
279
368
|
const limit = params.limit || 50;
|
|
@@ -284,13 +373,22 @@ var init_json_storage = __esm({
|
|
|
284
373
|
const agents = this.loadAgents();
|
|
285
374
|
let allRecords = [];
|
|
286
375
|
try {
|
|
287
|
-
const files = (0,
|
|
376
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
288
377
|
for (const file of files) {
|
|
289
|
-
const records = this.readJsonFile((0,
|
|
378
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
290
379
|
allRecords.push(...records);
|
|
291
380
|
}
|
|
292
381
|
} catch {
|
|
293
382
|
}
|
|
383
|
+
if (params.sessionId) {
|
|
384
|
+
allRecords = allRecords.filter((r) => r.session_id === params.sessionId);
|
|
385
|
+
}
|
|
386
|
+
if (params.agentId) {
|
|
387
|
+
const allowed = new Set(
|
|
388
|
+
[...sessions.values()].filter((s) => s.agent_id === params.agentId).map((s) => s.id)
|
|
389
|
+
);
|
|
390
|
+
allRecords = allRecords.filter((r) => allowed.has(r.session_id));
|
|
391
|
+
}
|
|
294
392
|
if (params.agentType) {
|
|
295
393
|
const allowed = new Set(
|
|
296
394
|
[...sessions.values()].filter((s) => (agents.get(s.agent_id)?.agent_type || "") === params.agentType).map((s) => s.id)
|
|
@@ -300,6 +398,15 @@ var init_json_storage = __esm({
|
|
|
300
398
|
if (params.decision) {
|
|
301
399
|
allRecords = allRecords.filter((r) => r.decision === params.decision);
|
|
302
400
|
}
|
|
401
|
+
if (params.riskLevel) {
|
|
402
|
+
allRecords = allRecords.filter((r) => r.risk_level === params.riskLevel);
|
|
403
|
+
}
|
|
404
|
+
if (params.fromTime) {
|
|
405
|
+
allRecords = allRecords.filter((r) => r.called_at >= params.fromTime);
|
|
406
|
+
}
|
|
407
|
+
if (params.toTime) {
|
|
408
|
+
allRecords = allRecords.filter((r) => r.called_at <= params.toTime);
|
|
409
|
+
}
|
|
303
410
|
return allRecords.length;
|
|
304
411
|
}
|
|
305
412
|
getStorageStats() {
|
|
@@ -307,12 +414,12 @@ var init_json_storage = __esm({
|
|
|
307
414
|
let jsonFiles = 0;
|
|
308
415
|
let jsonBytes = 0;
|
|
309
416
|
try {
|
|
310
|
-
const files = (0,
|
|
417
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
311
418
|
for (const file of files) {
|
|
312
|
-
const stat = (0,
|
|
419
|
+
const stat = (0, import_fs2.statSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
313
420
|
jsonFiles++;
|
|
314
421
|
jsonBytes += stat.size;
|
|
315
|
-
const records = this.readJsonFile((0,
|
|
422
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
316
423
|
toolCalls += records.length;
|
|
317
424
|
}
|
|
318
425
|
} catch {
|
|
@@ -322,9 +429,9 @@ var init_json_storage = __esm({
|
|
|
322
429
|
clearToolCalls() {
|
|
323
430
|
let removed = 0;
|
|
324
431
|
try {
|
|
325
|
-
const files = (0,
|
|
432
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
326
433
|
for (const file of files) {
|
|
327
|
-
(0,
|
|
434
|
+
(0, import_fs2.unlinkSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
328
435
|
removed++;
|
|
329
436
|
}
|
|
330
437
|
} catch {
|
|
@@ -342,6 +449,49 @@ var init_json_storage = __esm({
|
|
|
342
449
|
hostname: agent?.hostname || ""
|
|
343
450
|
};
|
|
344
451
|
}
|
|
452
|
+
/**
|
|
453
|
+
* 归档最老的记录:JSON 模式下每个 session 一个文件,这里把所有 session 的记录
|
|
454
|
+
* 汇总排序,最老的 (total - keep) 条写进 `<dataDir>/archive/*.jsonl.gz`,
|
|
455
|
+
* 再从各自的 session 文件里移除。数据不丢,只是不在主目录了。
|
|
456
|
+
*/
|
|
457
|
+
archiveOldRecords(keep) {
|
|
458
|
+
if (keep <= 0 || !(0, import_fs2.existsSync)(this.toolCallsDir)) return { archived: 0, file: "" };
|
|
459
|
+
const bySession = /* @__PURE__ */ new Map();
|
|
460
|
+
let total = 0;
|
|
461
|
+
for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
|
|
462
|
+
if (!f.endsWith(".json")) continue;
|
|
463
|
+
const sid = f.slice(0, -".json".length);
|
|
464
|
+
const recs = this.loadToolCalls(sid);
|
|
465
|
+
if (recs.length) {
|
|
466
|
+
bySession.set(sid, recs);
|
|
467
|
+
total += recs.length;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
471
|
+
const flat = [];
|
|
472
|
+
for (const [sid, recs] of bySession) for (const rec of recs) flat.push({ sid, rec });
|
|
473
|
+
flat.sort((a, b) => String(a.rec.called_at).localeCompare(String(b.rec.called_at)));
|
|
474
|
+
const archiveCount = total - keep;
|
|
475
|
+
const toArchive = flat.slice(0, archiveCount);
|
|
476
|
+
const archiveIds = new Set(toArchive.map((x) => String(x.rec.id)));
|
|
477
|
+
const { mkdirSync: mkdirSync4, writeFileSync: writeFileSync3 } = require("fs");
|
|
478
|
+
const { join: join4 } = require("path");
|
|
479
|
+
const { gzipSync } = require("zlib");
|
|
480
|
+
const archiveDir = join4(this.dataDir, "archive");
|
|
481
|
+
if (!(0, import_fs2.existsSync)(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
482
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
483
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
484
|
+
const payload = toArchive.map((x) => JSON.stringify(x.rec)).join("\n") + "\n";
|
|
485
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
486
|
+
for (const [sid, recs] of bySession) {
|
|
487
|
+
const remain = recs.filter((r) => !archiveIds.has(String(r.id)));
|
|
488
|
+
if (remain.length !== recs.length) this.saveToolCalls(sid, remain);
|
|
489
|
+
}
|
|
490
|
+
return { archived: toArchive.length, file };
|
|
491
|
+
}
|
|
492
|
+
/** JSON 模式没有需要回收的数据库页,归档已把文件挪走。 */
|
|
493
|
+
vacuum() {
|
|
494
|
+
}
|
|
345
495
|
getAgentsList(agentType) {
|
|
346
496
|
const agents = this.loadAgents();
|
|
347
497
|
const sessions = this.loadSessions();
|
|
@@ -376,9 +526,9 @@ var init_json_storage = __esm({
|
|
|
376
526
|
getTimeline(limit = 100) {
|
|
377
527
|
const buckets = /* @__PURE__ */ new Map();
|
|
378
528
|
try {
|
|
379
|
-
const files = (0,
|
|
529
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
380
530
|
for (const file of files) {
|
|
381
|
-
const records = this.readJsonFile((0,
|
|
531
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
382
532
|
for (const r of records) {
|
|
383
533
|
const minute = r.called_at.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, "$1T$2");
|
|
384
534
|
const bucket = buckets.get(minute) || { allow: 0, deny: 0 };
|
|
@@ -409,16 +559,17 @@ __export(sqlite_storage_exports, {
|
|
|
409
559
|
});
|
|
410
560
|
function getDefaultDbPath() {
|
|
411
561
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
412
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
413
|
-
return (0,
|
|
562
|
+
const xdgData = process.env.XDG_DATA_HOME || (0, import_path3.join)(home, ".local", "share");
|
|
563
|
+
return (0, import_path3.join)(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
414
564
|
}
|
|
415
|
-
var
|
|
565
|
+
var import_fs3, import_path3, SCHEMA, SqliteStorage;
|
|
416
566
|
var init_sqlite_storage = __esm({
|
|
417
567
|
"src/shared/storage/sqlite-storage.ts"() {
|
|
418
568
|
"use strict";
|
|
419
569
|
init_cjs_shims();
|
|
420
|
-
|
|
421
|
-
|
|
570
|
+
import_fs3 = require("fs");
|
|
571
|
+
import_path3 = require("path");
|
|
572
|
+
init_record_guard();
|
|
422
573
|
SCHEMA = `
|
|
423
574
|
CREATE TABLE IF NOT EXISTS agents (
|
|
424
575
|
id TEXT PRIMARY KEY,
|
|
@@ -478,8 +629,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
478
629
|
);
|
|
479
630
|
}
|
|
480
631
|
const path = dbPath || getDefaultDbPath();
|
|
481
|
-
const dir = (0,
|
|
482
|
-
if (!(0,
|
|
632
|
+
const dir = (0, import_path3.dirname)(path);
|
|
633
|
+
if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
|
|
483
634
|
this.dbPath = path;
|
|
484
635
|
this.db = new Database(path);
|
|
485
636
|
this.db.pragma("journal_mode = WAL");
|
|
@@ -513,6 +664,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
513
664
|
return row.next;
|
|
514
665
|
}
|
|
515
666
|
insertToolCall(record) {
|
|
667
|
+
record = prepareRecordForStorage(
|
|
668
|
+
record,
|
|
669
|
+
this.dbPath
|
|
670
|
+
).record;
|
|
516
671
|
const result = this.db.transaction((rec) => {
|
|
517
672
|
const row = this.db.prepare(
|
|
518
673
|
"SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
|
|
@@ -556,6 +711,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
556
711
|
conditions.push("tc.session_id = ?");
|
|
557
712
|
values.push(params.sessionId);
|
|
558
713
|
}
|
|
714
|
+
if (params.agentId) {
|
|
715
|
+
conditions.push("s.agent_id = ?");
|
|
716
|
+
values.push(params.agentId);
|
|
717
|
+
}
|
|
559
718
|
if (params.agentType) {
|
|
560
719
|
conditions.push("a.agent_type = ?");
|
|
561
720
|
values.push(params.agentType);
|
|
@@ -564,6 +723,18 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
564
723
|
conditions.push("tc.decision = ?");
|
|
565
724
|
values.push(params.decision);
|
|
566
725
|
}
|
|
726
|
+
if (params.riskLevel) {
|
|
727
|
+
conditions.push("tc.risk_level = ?");
|
|
728
|
+
values.push(params.riskLevel);
|
|
729
|
+
}
|
|
730
|
+
if (params.fromTime) {
|
|
731
|
+
conditions.push("tc.called_at >= ?");
|
|
732
|
+
values.push(params.fromTime);
|
|
733
|
+
}
|
|
734
|
+
if (params.toTime) {
|
|
735
|
+
conditions.push("tc.called_at <= ?");
|
|
736
|
+
values.push(params.toTime);
|
|
737
|
+
}
|
|
567
738
|
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
568
739
|
const limit = params.limit || 50;
|
|
569
740
|
const offset = params.offset || 0;
|
|
@@ -578,14 +749,34 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
578
749
|
countToolCalls(params) {
|
|
579
750
|
const conditions = [];
|
|
580
751
|
const values = [];
|
|
581
|
-
if (params.
|
|
582
|
-
conditions.push("tc.
|
|
583
|
-
values.push(params.
|
|
752
|
+
if (params.sessionId) {
|
|
753
|
+
conditions.push("tc.session_id = ?");
|
|
754
|
+
values.push(params.sessionId);
|
|
755
|
+
}
|
|
756
|
+
if (params.agentId) {
|
|
757
|
+
conditions.push("s.agent_id = ?");
|
|
758
|
+
values.push(params.agentId);
|
|
584
759
|
}
|
|
585
760
|
if (params.agentType) {
|
|
586
761
|
conditions.push("a.agent_type = ?");
|
|
587
762
|
values.push(params.agentType);
|
|
588
763
|
}
|
|
764
|
+
if (params.decision) {
|
|
765
|
+
conditions.push("tc.decision = ?");
|
|
766
|
+
values.push(params.decision);
|
|
767
|
+
}
|
|
768
|
+
if (params.riskLevel) {
|
|
769
|
+
conditions.push("tc.risk_level = ?");
|
|
770
|
+
values.push(params.riskLevel);
|
|
771
|
+
}
|
|
772
|
+
if (params.fromTime) {
|
|
773
|
+
conditions.push("tc.called_at >= ?");
|
|
774
|
+
values.push(params.fromTime);
|
|
775
|
+
}
|
|
776
|
+
if (params.toTime) {
|
|
777
|
+
conditions.push("tc.called_at <= ?");
|
|
778
|
+
values.push(params.toTime);
|
|
779
|
+
}
|
|
589
780
|
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
590
781
|
const row = this.db.prepare(
|
|
591
782
|
`SELECT COUNT(*) as total FROM tool_calls tc
|
|
@@ -617,6 +808,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
617
808
|
})();
|
|
618
809
|
return result;
|
|
619
810
|
}
|
|
811
|
+
/**
|
|
812
|
+
* 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
|
|
813
|
+
*
|
|
814
|
+
* 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
|
|
815
|
+
* 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
|
|
816
|
+
* 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
|
|
817
|
+
*/
|
|
818
|
+
archiveOldRecords(keep) {
|
|
819
|
+
if (keep <= 0) return { archived: 0, file: "" };
|
|
820
|
+
const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
|
|
821
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
822
|
+
const cutoff = this.db.prepare(
|
|
823
|
+
"SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
|
|
824
|
+
).get(total - keep - 1);
|
|
825
|
+
if (!cutoff) return { archived: 0, file: "" };
|
|
826
|
+
const rows = this.db.prepare(
|
|
827
|
+
"SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
|
|
828
|
+
).all(cutoff.id);
|
|
829
|
+
if (rows.length === 0) return { archived: 0, file: "" };
|
|
830
|
+
const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
|
|
831
|
+
const { dirname: dirname4, join: join4 } = require("path");
|
|
832
|
+
const { gzipSync } = require("zlib");
|
|
833
|
+
const archiveDir = join4(dirname4(this.dbPath), "archive");
|
|
834
|
+
if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
835
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
836
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
837
|
+
const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
838
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
839
|
+
const deleted = this.db.transaction(() => {
|
|
840
|
+
const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
|
|
841
|
+
this.db.prepare(`
|
|
842
|
+
UPDATE sessions SET tool_call_count = (
|
|
843
|
+
SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
|
|
844
|
+
)
|
|
845
|
+
`).run();
|
|
846
|
+
return Number(res.changes);
|
|
847
|
+
})();
|
|
848
|
+
return { archived: deleted, file };
|
|
849
|
+
}
|
|
850
|
+
/** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
|
|
851
|
+
vacuum() {
|
|
852
|
+
this.db.exec("VACUUM");
|
|
853
|
+
}
|
|
620
854
|
getAgentsList(agentType) {
|
|
621
855
|
const where = agentType ? "WHERE a.agent_type = ?" : "";
|
|
622
856
|
return this.db.prepare(`
|
|
@@ -712,12 +946,12 @@ function getDb(dbPath) {
|
|
|
712
946
|
try {
|
|
713
947
|
const Database = require("better-sqlite3");
|
|
714
948
|
const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
|
|
715
|
-
const { join: join4, dirname:
|
|
949
|
+
const { join: join4, dirname: dirname4 } = require("path");
|
|
716
950
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
717
951
|
const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
|
|
718
952
|
const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
719
953
|
const path = dbPath || defaultPath;
|
|
720
|
-
const dir =
|
|
954
|
+
const dir = dirname4(path);
|
|
721
955
|
if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
|
|
722
956
|
rawDb = new Database(path);
|
|
723
957
|
rawDb.pragma("journal_mode = WAL");
|