@kenhkl/agent-gate 0.9.0 → 0.9.2
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/dashboard.html +175 -3
- package/dist/shared/storage/db.d.ts +9 -0
- package/dist/shared/storage/db.js +228 -47
- package/dist/v1/cli.js +344 -54
- package/dist/v1/server.js +251 -54
- package/dist/v3/cli.js +295 -97
- 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 {
|
|
@@ -296,9 +373,9 @@ var init_json_storage = __esm({
|
|
|
296
373
|
const agents = this.loadAgents();
|
|
297
374
|
let allRecords = [];
|
|
298
375
|
try {
|
|
299
|
-
const files = (0,
|
|
376
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
300
377
|
for (const file of files) {
|
|
301
|
-
const records = this.readJsonFile((0,
|
|
378
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
302
379
|
allRecords.push(...records);
|
|
303
380
|
}
|
|
304
381
|
} catch {
|
|
@@ -337,12 +414,12 @@ var init_json_storage = __esm({
|
|
|
337
414
|
let jsonFiles = 0;
|
|
338
415
|
let jsonBytes = 0;
|
|
339
416
|
try {
|
|
340
|
-
const files = (0,
|
|
417
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
341
418
|
for (const file of files) {
|
|
342
|
-
const stat = (0,
|
|
419
|
+
const stat = (0, import_fs2.statSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
343
420
|
jsonFiles++;
|
|
344
421
|
jsonBytes += stat.size;
|
|
345
|
-
const records = this.readJsonFile((0,
|
|
422
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
346
423
|
toolCalls += records.length;
|
|
347
424
|
}
|
|
348
425
|
} catch {
|
|
@@ -352,9 +429,9 @@ var init_json_storage = __esm({
|
|
|
352
429
|
clearToolCalls() {
|
|
353
430
|
let removed = 0;
|
|
354
431
|
try {
|
|
355
|
-
const files = (0,
|
|
432
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
356
433
|
for (const file of files) {
|
|
357
|
-
(0,
|
|
434
|
+
(0, import_fs2.unlinkSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
358
435
|
removed++;
|
|
359
436
|
}
|
|
360
437
|
} catch {
|
|
@@ -372,6 +449,49 @@ var init_json_storage = __esm({
|
|
|
372
449
|
hostname: agent?.hostname || ""
|
|
373
450
|
};
|
|
374
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
|
+
}
|
|
375
495
|
getAgentsList(agentType) {
|
|
376
496
|
const agents = this.loadAgents();
|
|
377
497
|
const sessions = this.loadSessions();
|
|
@@ -406,9 +526,9 @@ var init_json_storage = __esm({
|
|
|
406
526
|
getTimeline(limit = 100) {
|
|
407
527
|
const buckets = /* @__PURE__ */ new Map();
|
|
408
528
|
try {
|
|
409
|
-
const files = (0,
|
|
529
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
410
530
|
for (const file of files) {
|
|
411
|
-
const records = this.readJsonFile((0,
|
|
531
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
412
532
|
for (const r of records) {
|
|
413
533
|
const minute = r.called_at.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, "$1T$2");
|
|
414
534
|
const bucket = buckets.get(minute) || { allow: 0, deny: 0 };
|
|
@@ -439,16 +559,17 @@ __export(sqlite_storage_exports, {
|
|
|
439
559
|
});
|
|
440
560
|
function getDefaultDbPath() {
|
|
441
561
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
442
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
443
|
-
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");
|
|
444
564
|
}
|
|
445
|
-
var
|
|
565
|
+
var import_fs3, import_path3, SCHEMA, SqliteStorage;
|
|
446
566
|
var init_sqlite_storage = __esm({
|
|
447
567
|
"src/shared/storage/sqlite-storage.ts"() {
|
|
448
568
|
"use strict";
|
|
449
569
|
init_cjs_shims();
|
|
450
|
-
|
|
451
|
-
|
|
570
|
+
import_fs3 = require("fs");
|
|
571
|
+
import_path3 = require("path");
|
|
572
|
+
init_record_guard();
|
|
452
573
|
SCHEMA = `
|
|
453
574
|
CREATE TABLE IF NOT EXISTS agents (
|
|
454
575
|
id TEXT PRIMARY KEY,
|
|
@@ -488,6 +609,7 @@ CREATE TABLE IF NOT EXISTS tool_calls (
|
|
|
488
609
|
reason TEXT DEFAULT '',
|
|
489
610
|
confidence INTEGER DEFAULT 0,
|
|
490
611
|
analysis_duration_ms REAL DEFAULT 0,
|
|
612
|
+
violation_graph TEXT DEFAULT '{}',
|
|
491
613
|
called_at TEXT DEFAULT (datetime('now')),
|
|
492
614
|
UNIQUE(session_id, seq)
|
|
493
615
|
);
|
|
@@ -508,12 +630,23 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
508
630
|
);
|
|
509
631
|
}
|
|
510
632
|
const path = dbPath || getDefaultDbPath();
|
|
511
|
-
const dir = (0,
|
|
512
|
-
if (!(0,
|
|
633
|
+
const dir = (0, import_path3.dirname)(path);
|
|
634
|
+
if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
|
|
513
635
|
this.dbPath = path;
|
|
514
636
|
this.db = new Database(path);
|
|
515
637
|
this.db.pragma("journal_mode = WAL");
|
|
516
638
|
this.db.exec(SCHEMA);
|
|
639
|
+
this.ensureColumn("tool_calls", "violation_graph", "violation_graph TEXT DEFAULT '{}'");
|
|
640
|
+
}
|
|
641
|
+
/** 幂等补列(旧数据库平滑升级,失败不阻断启动)。 */
|
|
642
|
+
ensureColumn(table, column, ddl) {
|
|
643
|
+
try {
|
|
644
|
+
const cols = this.db.prepare(`PRAGMA table_info(${table})`).all();
|
|
645
|
+
if (!cols.some((c) => c.name === column)) {
|
|
646
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
647
|
+
}
|
|
648
|
+
} catch {
|
|
649
|
+
}
|
|
517
650
|
}
|
|
518
651
|
static isAvailable() {
|
|
519
652
|
try {
|
|
@@ -543,6 +676,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
543
676
|
return row.next;
|
|
544
677
|
}
|
|
545
678
|
insertToolCall(record) {
|
|
679
|
+
record = prepareRecordForStorage(
|
|
680
|
+
record,
|
|
681
|
+
this.dbPath
|
|
682
|
+
).record;
|
|
546
683
|
const result = this.db.transaction((rec) => {
|
|
547
684
|
const row = this.db.prepare(
|
|
548
685
|
"SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
|
|
@@ -551,8 +688,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
551
688
|
return this.db.prepare(`
|
|
552
689
|
INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
|
|
553
690
|
action_type, action_detail, decision, risk_level, risk_type, violated_layers,
|
|
554
|
-
reason, confidence, analysis_duration_ms)
|
|
555
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
691
|
+
reason, confidence, analysis_duration_ms, violation_graph)
|
|
692
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
556
693
|
`).run(
|
|
557
694
|
rec.session_id,
|
|
558
695
|
seq,
|
|
@@ -568,7 +705,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
568
705
|
rec.violated_layers,
|
|
569
706
|
rec.reason,
|
|
570
707
|
rec.confidence,
|
|
571
|
-
rec.analysis_duration_ms
|
|
708
|
+
rec.analysis_duration_ms,
|
|
709
|
+
rec.violation_graph || "{}"
|
|
572
710
|
);
|
|
573
711
|
})(record);
|
|
574
712
|
return Number(result.lastInsertRowid);
|
|
@@ -683,6 +821,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
683
821
|
})();
|
|
684
822
|
return result;
|
|
685
823
|
}
|
|
824
|
+
/**
|
|
825
|
+
* 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
|
|
826
|
+
*
|
|
827
|
+
* 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
|
|
828
|
+
* 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
|
|
829
|
+
* 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
|
|
830
|
+
*/
|
|
831
|
+
archiveOldRecords(keep) {
|
|
832
|
+
if (keep <= 0) return { archived: 0, file: "" };
|
|
833
|
+
const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
|
|
834
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
835
|
+
const cutoff = this.db.prepare(
|
|
836
|
+
"SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
|
|
837
|
+
).get(total - keep - 1);
|
|
838
|
+
if (!cutoff) return { archived: 0, file: "" };
|
|
839
|
+
const rows = this.db.prepare(
|
|
840
|
+
"SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
|
|
841
|
+
).all(cutoff.id);
|
|
842
|
+
if (rows.length === 0) return { archived: 0, file: "" };
|
|
843
|
+
const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
|
|
844
|
+
const { dirname: dirname4, join: join4 } = require("path");
|
|
845
|
+
const { gzipSync } = require("zlib");
|
|
846
|
+
const archiveDir = join4(dirname4(this.dbPath), "archive");
|
|
847
|
+
if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
848
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
849
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
850
|
+
const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
851
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
852
|
+
const deleted = this.db.transaction(() => {
|
|
853
|
+
const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
|
|
854
|
+
this.db.prepare(`
|
|
855
|
+
UPDATE sessions SET tool_call_count = (
|
|
856
|
+
SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
|
|
857
|
+
)
|
|
858
|
+
`).run();
|
|
859
|
+
return Number(res.changes);
|
|
860
|
+
})();
|
|
861
|
+
return { archived: deleted, file };
|
|
862
|
+
}
|
|
863
|
+
/** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
|
|
864
|
+
vacuum() {
|
|
865
|
+
this.db.exec("VACUUM");
|
|
866
|
+
}
|
|
686
867
|
getAgentsList(agentType) {
|
|
687
868
|
const where = agentType ? "WHERE a.agent_type = ?" : "";
|
|
688
869
|
return this.db.prepare(`
|
|
@@ -778,12 +959,12 @@ function getDb(dbPath) {
|
|
|
778
959
|
try {
|
|
779
960
|
const Database = require("better-sqlite3");
|
|
780
961
|
const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
|
|
781
|
-
const { join: join4, dirname:
|
|
962
|
+
const { join: join4, dirname: dirname4 } = require("path");
|
|
782
963
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
783
964
|
const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
|
|
784
965
|
const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
785
966
|
const path = dbPath || defaultPath;
|
|
786
|
-
const dir =
|
|
967
|
+
const dir = dirname4(path);
|
|
787
968
|
if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
|
|
788
969
|
rawDb = new Database(path);
|
|
789
970
|
rawDb.pragma("journal_mode = WAL");
|