@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
package/dist/v1/server.js
CHANGED
|
@@ -52,15 +52,29 @@ function sessionHistoryLimit() {
|
|
|
52
52
|
const v = Number.isFinite(n) ? n : DEFAULT_MAX_SESSIONS;
|
|
53
53
|
return Math.max(1, Math.min(v, 50));
|
|
54
54
|
}
|
|
55
|
-
function buildSessionHistory(queryToolCalls2, sessionId) {
|
|
55
|
+
function buildSessionHistory(queryToolCalls2, sessionId, listAgentTypes) {
|
|
56
56
|
if (!isMultiSessionEnabled()) return [];
|
|
57
57
|
const maxSessions = sessionHistoryLimit();
|
|
58
58
|
try {
|
|
59
59
|
const scanLimit = Math.max(DEFAULT_HISTORY_LIMIT, maxSessions * DEFAULT_ACTIONS_PER_SESSION);
|
|
60
|
-
const
|
|
60
|
+
const agentTypes = listAgentTypes?.() ?? [];
|
|
61
|
+
let recent;
|
|
62
|
+
if (agentTypes.length > 1) {
|
|
63
|
+
const perAgent = Math.max(
|
|
64
|
+
DEFAULT_ACTIONS_PER_SESSION,
|
|
65
|
+
Math.floor(DEFAULT_HISTORY_LIMIT / agentTypes.length)
|
|
66
|
+
);
|
|
67
|
+
recent = agentTypes.flatMap((a) => queryToolCalls2({ limit: perAgent, agentType: a }));
|
|
68
|
+
} else {
|
|
69
|
+
recent = queryToolCalls2({ limit: scanLimit });
|
|
70
|
+
}
|
|
61
71
|
const bySession = /* @__PURE__ */ new Map();
|
|
72
|
+
const sessionAgent = /* @__PURE__ */ new Map();
|
|
62
73
|
for (const tc of recent) {
|
|
63
74
|
if (tc.session_id === sessionId) continue;
|
|
75
|
+
if (tc.agent_type && !sessionAgent.has(tc.session_id)) {
|
|
76
|
+
sessionAgent.set(tc.session_id, tc.agent_type);
|
|
77
|
+
}
|
|
64
78
|
const list = bySession.get(tc.session_id) || [];
|
|
65
79
|
list.unshift({
|
|
66
80
|
action_type: tc.action_type,
|
|
@@ -71,6 +85,7 @@ function buildSessionHistory(queryToolCalls2, sessionId) {
|
|
|
71
85
|
}
|
|
72
86
|
return [...bySession.entries()].slice(0, maxSessions).map(([sid, actions]) => ({
|
|
73
87
|
session_id: sid,
|
|
88
|
+
...sessionAgent.get(sid) ? { agent_id: sessionAgent.get(sid) } : {},
|
|
74
89
|
action_history: actions.slice(0, DEFAULT_ACTIONS_PER_SESSION)
|
|
75
90
|
}));
|
|
76
91
|
} catch {
|
|
@@ -346,6 +361,12 @@ function bindAgent(principal, agentType) {
|
|
|
346
361
|
function maxBodyBytes() {
|
|
347
362
|
return envInt("AGENT_GATE_MAX_BODY_BYTES", 16 * 1024 * 1024);
|
|
348
363
|
}
|
|
364
|
+
function maxRecordBytes() {
|
|
365
|
+
return envInt("AGENT_GATE_MAX_RECORD_BYTES", 1024 * 1024);
|
|
366
|
+
}
|
|
367
|
+
function diskDegradePercent() {
|
|
368
|
+
return envInt("AGENT_GATE_DISK_DEGRADE_PERCENT", 2);
|
|
369
|
+
}
|
|
349
370
|
function rateLimitQps() {
|
|
350
371
|
return envInt("AGENT_GATE_RATE_LIMIT_QPS", 100);
|
|
351
372
|
}
|
|
@@ -622,6 +643,66 @@ var init_moss_client = __esm({
|
|
|
622
643
|
}
|
|
623
644
|
});
|
|
624
645
|
|
|
646
|
+
// src/shared/storage/record-guard.ts
|
|
647
|
+
function clipText(value, limit = maxRecordBytes()) {
|
|
648
|
+
const s = String(value ?? "");
|
|
649
|
+
if (limit <= 0 || s.length <= limit) return s;
|
|
650
|
+
const head = s.slice(0, limit);
|
|
651
|
+
const digest = (0, import_crypto.createHash)("sha256").update(s).digest("hex").slice(0, 16);
|
|
652
|
+
return `${head}\u2026[truncated ${s.length - limit}B sha256:${digest}]`;
|
|
653
|
+
}
|
|
654
|
+
function diskFreePercent(targetPath) {
|
|
655
|
+
try {
|
|
656
|
+
const st = (0, import_fs.statfsSync)((0, import_path.dirname)(targetPath));
|
|
657
|
+
const total = Number(st.blocks) || 0;
|
|
658
|
+
const avail = Number(st.bavail) || 0;
|
|
659
|
+
if (!total) return 100;
|
|
660
|
+
return avail / total * 100;
|
|
661
|
+
} catch {
|
|
662
|
+
return 100;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
function shouldDegradeWrites(targetPath) {
|
|
666
|
+
const degrade = diskDegradePercent();
|
|
667
|
+
if (degrade <= 0) return false;
|
|
668
|
+
return diskFreePercent(targetPath) < degrade;
|
|
669
|
+
}
|
|
670
|
+
function degradeRecord(rec) {
|
|
671
|
+
const command = String(rec.command ?? "");
|
|
672
|
+
const clipped = command.length > DEGRADE_COMMAND_BYTES ? `${command.slice(0, DEGRADE_COMMAND_BYTES)}\u2026[degraded]` : command;
|
|
673
|
+
return {
|
|
674
|
+
...rec,
|
|
675
|
+
command: clipped,
|
|
676
|
+
action_detail: String(rec.action_detail ?? "") > "" ? String(rec.action_detail).slice(0, DEGRADE_COMMAND_BYTES) : "",
|
|
677
|
+
description: "",
|
|
678
|
+
reason: String(rec.reason ?? "").slice(0, 256)
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
function prepareRecordForStorage(rec, targetPath) {
|
|
682
|
+
let out = {
|
|
683
|
+
...rec,
|
|
684
|
+
command: clipText(rec.command),
|
|
685
|
+
action_detail: clipText(rec.action_detail),
|
|
686
|
+
reason: clipText(rec.reason, 4096),
|
|
687
|
+
description: clipText(rec.description, 4096)
|
|
688
|
+
};
|
|
689
|
+
const degraded = shouldDegradeWrites(targetPath);
|
|
690
|
+
if (degraded) out = degradeRecord(out);
|
|
691
|
+
return { record: out, degraded };
|
|
692
|
+
}
|
|
693
|
+
var import_crypto, import_fs, import_path, DEGRADE_COMMAND_BYTES;
|
|
694
|
+
var init_record_guard = __esm({
|
|
695
|
+
"src/shared/storage/record-guard.ts"() {
|
|
696
|
+
"use strict";
|
|
697
|
+
init_cjs_shims();
|
|
698
|
+
import_crypto = require("crypto");
|
|
699
|
+
import_fs = require("fs");
|
|
700
|
+
import_path = require("path");
|
|
701
|
+
init_security();
|
|
702
|
+
DEGRADE_COMMAND_BYTES = 512;
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
|
|
625
706
|
// src/shared/storage/json-storage.ts
|
|
626
707
|
var json_storage_exports = {};
|
|
627
708
|
__export(json_storage_exports, {
|
|
@@ -632,17 +713,18 @@ function nowISO() {
|
|
|
632
713
|
}
|
|
633
714
|
function getDefaultDataDir() {
|
|
634
715
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
635
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
636
|
-
return (0,
|
|
716
|
+
const xdgData = process.env.XDG_DATA_HOME || (0, import_path2.join)(home, ".local", "share");
|
|
717
|
+
return (0, import_path2.join)(xdgData, "agent_gate", "storage");
|
|
637
718
|
}
|
|
638
|
-
var
|
|
719
|
+
var import_fs2, import_path2, JsonStorage;
|
|
639
720
|
var init_json_storage = __esm({
|
|
640
721
|
"src/shared/storage/json-storage.ts"() {
|
|
641
722
|
"use strict";
|
|
642
723
|
init_cjs_shims();
|
|
643
|
-
|
|
644
|
-
|
|
724
|
+
import_fs2 = require("fs");
|
|
725
|
+
import_path2 = require("path");
|
|
645
726
|
init_security();
|
|
727
|
+
init_record_guard();
|
|
646
728
|
JsonStorage = class {
|
|
647
729
|
dataDir;
|
|
648
730
|
agentsPath;
|
|
@@ -655,11 +737,11 @@ var init_json_storage = __esm({
|
|
|
655
737
|
sessionLocks = /* @__PURE__ */ new Map();
|
|
656
738
|
constructor(dataDir) {
|
|
657
739
|
this.dataDir = dataDir || process.env.AGENT_GATE_DATA_DIR || getDefaultDataDir();
|
|
658
|
-
this.agentsPath = (0,
|
|
659
|
-
this.sessionsPath = (0,
|
|
660
|
-
this.toolCallsDir = (0,
|
|
661
|
-
if (!(0,
|
|
662
|
-
if (!(0,
|
|
740
|
+
this.agentsPath = (0, import_path2.join)(this.dataDir, "agents.json");
|
|
741
|
+
this.sessionsPath = (0, import_path2.join)(this.dataDir, "sessions.json");
|
|
742
|
+
this.toolCallsDir = (0, import_path2.join)(this.dataDir, "tool_calls");
|
|
743
|
+
if (!(0, import_fs2.existsSync)(this.dataDir)) (0, import_fs2.mkdirSync)(this.dataDir, { recursive: true, mode: 448 });
|
|
744
|
+
if (!(0, import_fs2.existsSync)(this.toolCallsDir)) (0, import_fs2.mkdirSync)(this.toolCallsDir, { recursive: true, mode: 448 });
|
|
663
745
|
this.hardenStoragePermissions();
|
|
664
746
|
this.nextId = this.recoverNextId();
|
|
665
747
|
}
|
|
@@ -671,10 +753,10 @@ var init_json_storage = __esm({
|
|
|
671
753
|
const uid = process.getuid?.();
|
|
672
754
|
const tighten = (p, mode) => {
|
|
673
755
|
try {
|
|
674
|
-
const st = (0,
|
|
756
|
+
const st = (0, import_fs2.statSync)(p);
|
|
675
757
|
if (st.isSymbolicLink()) return;
|
|
676
758
|
if (uid !== void 0 && st.uid !== uid) return;
|
|
677
|
-
if ((st.mode & 511) !== mode) (0,
|
|
759
|
+
if ((st.mode & 511) !== mode) (0, import_fs2.chmodSync)(p, mode);
|
|
678
760
|
} catch {
|
|
679
761
|
}
|
|
680
762
|
};
|
|
@@ -683,9 +765,9 @@ var init_json_storage = __esm({
|
|
|
683
765
|
tighten(this.agentsPath, 384);
|
|
684
766
|
tighten(this.sessionsPath, 384);
|
|
685
767
|
try {
|
|
686
|
-
for (const f of (0,
|
|
768
|
+
for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
|
|
687
769
|
if (f.endsWith(".json") || f.endsWith(".tmp")) {
|
|
688
|
-
tighten((0,
|
|
770
|
+
tighten((0, import_path2.join)(this.toolCallsDir, f), 384);
|
|
689
771
|
}
|
|
690
772
|
}
|
|
691
773
|
} catch {
|
|
@@ -695,9 +777,9 @@ var init_json_storage = __esm({
|
|
|
695
777
|
recoverNextId() {
|
|
696
778
|
let maxId = 0;
|
|
697
779
|
try {
|
|
698
|
-
const files = (0,
|
|
780
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
699
781
|
for (const file of files) {
|
|
700
|
-
const records = this.readJsonFile((0,
|
|
782
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
701
783
|
for (const r of records) {
|
|
702
784
|
if (r.id > maxId) maxId = r.id;
|
|
703
785
|
}
|
|
@@ -708,19 +790,19 @@ var init_json_storage = __esm({
|
|
|
708
790
|
}
|
|
709
791
|
readJsonFile(path, fallback) {
|
|
710
792
|
try {
|
|
711
|
-
if (!(0,
|
|
712
|
-
return JSON.parse((0,
|
|
793
|
+
if (!(0, import_fs2.existsSync)(path)) return fallback;
|
|
794
|
+
return JSON.parse((0, import_fs2.readFileSync)(path, "utf-8"));
|
|
713
795
|
} catch {
|
|
714
796
|
return fallback;
|
|
715
797
|
}
|
|
716
798
|
}
|
|
717
799
|
/** 原子写入:先写 .tmp 再 rename(rename 保留 tmp 的 0600 权限) */
|
|
718
800
|
writeJsonFile(path, data) {
|
|
719
|
-
const dir = (0,
|
|
720
|
-
if (!(0,
|
|
801
|
+
const dir = (0, import_path2.dirname)(path);
|
|
802
|
+
if (!(0, import_fs2.existsSync)(dir)) (0, import_fs2.mkdirSync)(dir, { recursive: true, mode: 448 });
|
|
721
803
|
const tmp = path + ".tmp";
|
|
722
|
-
(0,
|
|
723
|
-
(0,
|
|
804
|
+
(0, import_fs2.writeFileSync)(tmp, JSON.stringify(data, null, 2), { encoding: "utf-8", mode: 384 });
|
|
805
|
+
(0, import_fs2.renameSync)(tmp, path);
|
|
724
806
|
}
|
|
725
807
|
loadAgents() {
|
|
726
808
|
const raw = this.readJsonFile(this.agentsPath, {});
|
|
@@ -738,12 +820,12 @@ var init_json_storage = __esm({
|
|
|
738
820
|
}
|
|
739
821
|
loadToolCalls(sessionId) {
|
|
740
822
|
return this.readJsonFile(
|
|
741
|
-
(0,
|
|
823
|
+
(0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`),
|
|
742
824
|
[]
|
|
743
825
|
);
|
|
744
826
|
}
|
|
745
827
|
saveToolCalls(sessionId, records) {
|
|
746
|
-
this.writeJsonFile((0,
|
|
828
|
+
this.writeJsonFile((0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`), records);
|
|
747
829
|
}
|
|
748
830
|
getOrCreateAgent(agentType, hostname, version2 = "") {
|
|
749
831
|
const agents = this.loadAgents();
|
|
@@ -788,6 +870,10 @@ var init_json_storage = __esm({
|
|
|
788
870
|
return Math.max(...records.map((r) => r.seq)) + 1;
|
|
789
871
|
}
|
|
790
872
|
insertToolCall(record) {
|
|
873
|
+
record = prepareRecordForStorage(
|
|
874
|
+
record,
|
|
875
|
+
this.dataDir
|
|
876
|
+
).record;
|
|
791
877
|
const sessionId = record.session_id;
|
|
792
878
|
const prev = this.sessionLocks.get(sessionId) ?? Promise.resolve();
|
|
793
879
|
const next = prev.then(() => {
|
|
@@ -825,9 +911,9 @@ var init_json_storage = __esm({
|
|
|
825
911
|
allRecords = records.map((r) => this.enrichRecord(r, sessions, agents));
|
|
826
912
|
} else {
|
|
827
913
|
try {
|
|
828
|
-
const files = (0,
|
|
914
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
829
915
|
for (const file of files) {
|
|
830
|
-
const records = this.readJsonFile((0,
|
|
916
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
831
917
|
allRecords.push(...records.map((r) => this.enrichRecord(r, sessions, agents)));
|
|
832
918
|
}
|
|
833
919
|
} catch {
|
|
@@ -861,9 +947,9 @@ var init_json_storage = __esm({
|
|
|
861
947
|
const agents = this.loadAgents();
|
|
862
948
|
let allRecords = [];
|
|
863
949
|
try {
|
|
864
|
-
const files = (0,
|
|
950
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
865
951
|
for (const file of files) {
|
|
866
|
-
const records = this.readJsonFile((0,
|
|
952
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
867
953
|
allRecords.push(...records);
|
|
868
954
|
}
|
|
869
955
|
} catch {
|
|
@@ -902,12 +988,12 @@ var init_json_storage = __esm({
|
|
|
902
988
|
let jsonFiles = 0;
|
|
903
989
|
let jsonBytes = 0;
|
|
904
990
|
try {
|
|
905
|
-
const files = (0,
|
|
991
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
906
992
|
for (const file of files) {
|
|
907
|
-
const stat = (0,
|
|
993
|
+
const stat = (0, import_fs2.statSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
908
994
|
jsonFiles++;
|
|
909
995
|
jsonBytes += stat.size;
|
|
910
|
-
const records = this.readJsonFile((0,
|
|
996
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
911
997
|
toolCalls += records.length;
|
|
912
998
|
}
|
|
913
999
|
} catch {
|
|
@@ -917,9 +1003,9 @@ var init_json_storage = __esm({
|
|
|
917
1003
|
clearToolCalls() {
|
|
918
1004
|
let removed = 0;
|
|
919
1005
|
try {
|
|
920
|
-
const files = (0,
|
|
1006
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
921
1007
|
for (const file of files) {
|
|
922
|
-
(0,
|
|
1008
|
+
(0, import_fs2.unlinkSync)((0, import_path2.join)(this.toolCallsDir, file));
|
|
923
1009
|
removed++;
|
|
924
1010
|
}
|
|
925
1011
|
} catch {
|
|
@@ -937,6 +1023,49 @@ var init_json_storage = __esm({
|
|
|
937
1023
|
hostname: agent?.hostname || ""
|
|
938
1024
|
};
|
|
939
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* 归档最老的记录:JSON 模式下每个 session 一个文件,这里把所有 session 的记录
|
|
1028
|
+
* 汇总排序,最老的 (total - keep) 条写进 `<dataDir>/archive/*.jsonl.gz`,
|
|
1029
|
+
* 再从各自的 session 文件里移除。数据不丢,只是不在主目录了。
|
|
1030
|
+
*/
|
|
1031
|
+
archiveOldRecords(keep) {
|
|
1032
|
+
if (keep <= 0 || !(0, import_fs2.existsSync)(this.toolCallsDir)) return { archived: 0, file: "" };
|
|
1033
|
+
const bySession = /* @__PURE__ */ new Map();
|
|
1034
|
+
let total = 0;
|
|
1035
|
+
for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
|
|
1036
|
+
if (!f.endsWith(".json")) continue;
|
|
1037
|
+
const sid = f.slice(0, -".json".length);
|
|
1038
|
+
const recs = this.loadToolCalls(sid);
|
|
1039
|
+
if (recs.length) {
|
|
1040
|
+
bySession.set(sid, recs);
|
|
1041
|
+
total += recs.length;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
1045
|
+
const flat = [];
|
|
1046
|
+
for (const [sid, recs] of bySession) for (const rec of recs) flat.push({ sid, rec });
|
|
1047
|
+
flat.sort((a, b) => String(a.rec.called_at).localeCompare(String(b.rec.called_at)));
|
|
1048
|
+
const archiveCount = total - keep;
|
|
1049
|
+
const toArchive = flat.slice(0, archiveCount);
|
|
1050
|
+
const archiveIds = new Set(toArchive.map((x) => String(x.rec.id)));
|
|
1051
|
+
const { mkdirSync: mkdirSync4, writeFileSync: writeFileSync3 } = require("fs");
|
|
1052
|
+
const { join: join4 } = require("path");
|
|
1053
|
+
const { gzipSync } = require("zlib");
|
|
1054
|
+
const archiveDir = join4(this.dataDir, "archive");
|
|
1055
|
+
if (!(0, import_fs2.existsSync)(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
1056
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1057
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1058
|
+
const payload = toArchive.map((x) => JSON.stringify(x.rec)).join("\n") + "\n";
|
|
1059
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1060
|
+
for (const [sid, recs] of bySession) {
|
|
1061
|
+
const remain = recs.filter((r) => !archiveIds.has(String(r.id)));
|
|
1062
|
+
if (remain.length !== recs.length) this.saveToolCalls(sid, remain);
|
|
1063
|
+
}
|
|
1064
|
+
return { archived: toArchive.length, file };
|
|
1065
|
+
}
|
|
1066
|
+
/** JSON 模式没有需要回收的数据库页,归档已把文件挪走。 */
|
|
1067
|
+
vacuum() {
|
|
1068
|
+
}
|
|
940
1069
|
getAgentsList(agentType) {
|
|
941
1070
|
const agents = this.loadAgents();
|
|
942
1071
|
const sessions = this.loadSessions();
|
|
@@ -971,9 +1100,9 @@ var init_json_storage = __esm({
|
|
|
971
1100
|
getTimeline(limit = 100) {
|
|
972
1101
|
const buckets = /* @__PURE__ */ new Map();
|
|
973
1102
|
try {
|
|
974
|
-
const files = (0,
|
|
1103
|
+
const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
|
|
975
1104
|
for (const file of files) {
|
|
976
|
-
const records = this.readJsonFile((0,
|
|
1105
|
+
const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
|
|
977
1106
|
for (const r of records) {
|
|
978
1107
|
const minute = r.called_at.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, "$1T$2");
|
|
979
1108
|
const bucket = buckets.get(minute) || { allow: 0, deny: 0 };
|
|
@@ -1004,16 +1133,17 @@ __export(sqlite_storage_exports, {
|
|
|
1004
1133
|
});
|
|
1005
1134
|
function getDefaultDbPath() {
|
|
1006
1135
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
1007
|
-
const xdgData = process.env.XDG_DATA_HOME || (0,
|
|
1008
|
-
return (0,
|
|
1136
|
+
const xdgData = process.env.XDG_DATA_HOME || (0, import_path3.join)(home, ".local", "share");
|
|
1137
|
+
return (0, import_path3.join)(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
1009
1138
|
}
|
|
1010
|
-
var
|
|
1139
|
+
var import_fs3, import_path3, SCHEMA, SqliteStorage;
|
|
1011
1140
|
var init_sqlite_storage = __esm({
|
|
1012
1141
|
"src/shared/storage/sqlite-storage.ts"() {
|
|
1013
1142
|
"use strict";
|
|
1014
1143
|
init_cjs_shims();
|
|
1015
|
-
|
|
1016
|
-
|
|
1144
|
+
import_fs3 = require("fs");
|
|
1145
|
+
import_path3 = require("path");
|
|
1146
|
+
init_record_guard();
|
|
1017
1147
|
SCHEMA = `
|
|
1018
1148
|
CREATE TABLE IF NOT EXISTS agents (
|
|
1019
1149
|
id TEXT PRIMARY KEY,
|
|
@@ -1053,6 +1183,7 @@ CREATE TABLE IF NOT EXISTS tool_calls (
|
|
|
1053
1183
|
reason TEXT DEFAULT '',
|
|
1054
1184
|
confidence INTEGER DEFAULT 0,
|
|
1055
1185
|
analysis_duration_ms REAL DEFAULT 0,
|
|
1186
|
+
violation_graph TEXT DEFAULT '{}',
|
|
1056
1187
|
called_at TEXT DEFAULT (datetime('now')),
|
|
1057
1188
|
UNIQUE(session_id, seq)
|
|
1058
1189
|
);
|
|
@@ -1073,12 +1204,23 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1073
1204
|
);
|
|
1074
1205
|
}
|
|
1075
1206
|
const path = dbPath || getDefaultDbPath();
|
|
1076
|
-
const dir = (0,
|
|
1077
|
-
if (!(0,
|
|
1207
|
+
const dir = (0, import_path3.dirname)(path);
|
|
1208
|
+
if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
|
|
1078
1209
|
this.dbPath = path;
|
|
1079
1210
|
this.db = new Database(path);
|
|
1080
1211
|
this.db.pragma("journal_mode = WAL");
|
|
1081
1212
|
this.db.exec(SCHEMA);
|
|
1213
|
+
this.ensureColumn("tool_calls", "violation_graph", "violation_graph TEXT DEFAULT '{}'");
|
|
1214
|
+
}
|
|
1215
|
+
/** 幂等补列(旧数据库平滑升级,失败不阻断启动)。 */
|
|
1216
|
+
ensureColumn(table, column, ddl) {
|
|
1217
|
+
try {
|
|
1218
|
+
const cols = this.db.prepare(`PRAGMA table_info(${table})`).all();
|
|
1219
|
+
if (!cols.some((c) => c.name === column)) {
|
|
1220
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
1221
|
+
}
|
|
1222
|
+
} catch {
|
|
1223
|
+
}
|
|
1082
1224
|
}
|
|
1083
1225
|
static isAvailable() {
|
|
1084
1226
|
try {
|
|
@@ -1108,6 +1250,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1108
1250
|
return row.next;
|
|
1109
1251
|
}
|
|
1110
1252
|
insertToolCall(record) {
|
|
1253
|
+
record = prepareRecordForStorage(
|
|
1254
|
+
record,
|
|
1255
|
+
this.dbPath
|
|
1256
|
+
).record;
|
|
1111
1257
|
const result = this.db.transaction((rec) => {
|
|
1112
1258
|
const row = this.db.prepare(
|
|
1113
1259
|
"SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
|
|
@@ -1116,8 +1262,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1116
1262
|
return this.db.prepare(`
|
|
1117
1263
|
INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
|
|
1118
1264
|
action_type, action_detail, decision, risk_level, risk_type, violated_layers,
|
|
1119
|
-
reason, confidence, analysis_duration_ms)
|
|
1120
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1265
|
+
reason, confidence, analysis_duration_ms, violation_graph)
|
|
1266
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1121
1267
|
`).run(
|
|
1122
1268
|
rec.session_id,
|
|
1123
1269
|
seq,
|
|
@@ -1133,7 +1279,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1133
1279
|
rec.violated_layers,
|
|
1134
1280
|
rec.reason,
|
|
1135
1281
|
rec.confidence,
|
|
1136
|
-
rec.analysis_duration_ms
|
|
1282
|
+
rec.analysis_duration_ms,
|
|
1283
|
+
rec.violation_graph || "{}"
|
|
1137
1284
|
);
|
|
1138
1285
|
})(record);
|
|
1139
1286
|
return Number(result.lastInsertRowid);
|
|
@@ -1248,6 +1395,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
|
|
|
1248
1395
|
})();
|
|
1249
1396
|
return result;
|
|
1250
1397
|
}
|
|
1398
|
+
/**
|
|
1399
|
+
* 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
|
|
1400
|
+
*
|
|
1401
|
+
* 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
|
|
1402
|
+
* 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
|
|
1403
|
+
* 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
|
|
1404
|
+
*/
|
|
1405
|
+
archiveOldRecords(keep) {
|
|
1406
|
+
if (keep <= 0) return { archived: 0, file: "" };
|
|
1407
|
+
const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
|
|
1408
|
+
if (total <= keep) return { archived: 0, file: "" };
|
|
1409
|
+
const cutoff = this.db.prepare(
|
|
1410
|
+
"SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
|
|
1411
|
+
).get(total - keep - 1);
|
|
1412
|
+
if (!cutoff) return { archived: 0, file: "" };
|
|
1413
|
+
const rows = this.db.prepare(
|
|
1414
|
+
"SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
|
|
1415
|
+
).all(cutoff.id);
|
|
1416
|
+
if (rows.length === 0) return { archived: 0, file: "" };
|
|
1417
|
+
const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
|
|
1418
|
+
const { dirname: dirname4, join: join4 } = require("path");
|
|
1419
|
+
const { gzipSync } = require("zlib");
|
|
1420
|
+
const archiveDir = join4(dirname4(this.dbPath), "archive");
|
|
1421
|
+
if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
|
|
1422
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
1423
|
+
const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
|
|
1424
|
+
const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
1425
|
+
writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
|
|
1426
|
+
const deleted = this.db.transaction(() => {
|
|
1427
|
+
const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
|
|
1428
|
+
this.db.prepare(`
|
|
1429
|
+
UPDATE sessions SET tool_call_count = (
|
|
1430
|
+
SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
|
|
1431
|
+
)
|
|
1432
|
+
`).run();
|
|
1433
|
+
return Number(res.changes);
|
|
1434
|
+
})();
|
|
1435
|
+
return { archived: deleted, file };
|
|
1436
|
+
}
|
|
1437
|
+
/** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
|
|
1438
|
+
vacuum() {
|
|
1439
|
+
this.db.exec("VACUUM");
|
|
1440
|
+
}
|
|
1251
1441
|
getAgentsList(agentType) {
|
|
1252
1442
|
const where = agentType ? "WHERE a.agent_type = ?" : "";
|
|
1253
1443
|
return this.db.prepare(`
|
|
@@ -1339,12 +1529,12 @@ function getDb(dbPath) {
|
|
|
1339
1529
|
try {
|
|
1340
1530
|
const Database = require("better-sqlite3");
|
|
1341
1531
|
const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
|
|
1342
|
-
const { join: join4, dirname:
|
|
1532
|
+
const { join: join4, dirname: dirname4 } = require("path");
|
|
1343
1533
|
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
1344
1534
|
const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
|
|
1345
1535
|
const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
|
|
1346
1536
|
const path = dbPath || defaultPath;
|
|
1347
|
-
const dir =
|
|
1537
|
+
const dir = dirname4(path);
|
|
1348
1538
|
if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
|
|
1349
1539
|
rawDb = new Database(path);
|
|
1350
1540
|
rawDb.pragma("journal_mode = WAL");
|
|
@@ -1409,7 +1599,7 @@ var import_zod = require("zod");
|
|
|
1409
1599
|
|
|
1410
1600
|
// src/version.ts
|
|
1411
1601
|
init_cjs_shims();
|
|
1412
|
-
var _version = true ? "0.9.
|
|
1602
|
+
var _version = true ? "0.9.2" : (() => {
|
|
1413
1603
|
try {
|
|
1414
1604
|
const require2 = createRequire(importMetaUrl);
|
|
1415
1605
|
const pkg = require2("../package.json");
|
|
@@ -1467,6 +1657,8 @@ var AgentGateResponseSchema = import_zod.z.object({
|
|
|
1467
1657
|
violated_layers: import_zod.z.array(import_zod.z.string()).default([]),
|
|
1468
1658
|
confidence: import_zod.z.number().int().min(0).max(100).default(100),
|
|
1469
1659
|
analysis_duration_ms: import_zod.z.number().default(0),
|
|
1660
|
+
// 结构化攻击链图(组合式拦截时非空;前端弹窗画图用)
|
|
1661
|
+
violation_graph: import_zod.z.any().nullable().optional(),
|
|
1470
1662
|
oversize: import_zod.z.boolean().optional().describe("\u672C\u5730\u5B58\u50A8\u662F\u5426\u8D85\u8FC7\u6E05\u7406\u9608\u503C\uFF08agg \u7528\uFF09"),
|
|
1471
1663
|
thresholdMB: import_zod.z.number().optional().describe("\u5B58\u50A8\u6E05\u7406\u9608\u503C MB\uFF08agg \u7528\uFF09"),
|
|
1472
1664
|
collected_context: import_zod.z.object({
|
|
@@ -1745,7 +1937,9 @@ async function analyzeRoute(c) {
|
|
|
1745
1937
|
if (isMultiSessionEnabled()) {
|
|
1746
1938
|
input.session_history = buildSessionHistory(
|
|
1747
1939
|
(params) => queryToolCalls(params),
|
|
1748
|
-
input.session_id
|
|
1940
|
+
input.session_id,
|
|
1941
|
+
// 多 agent 时按 agent 分组取历史,避免高频 agent 把别人的动作挤出窗口
|
|
1942
|
+
() => getAgentsList().map((a) => a.agent_type)
|
|
1749
1943
|
);
|
|
1750
1944
|
}
|
|
1751
1945
|
const mossRequest = normalize(input, context);
|
|
@@ -1771,7 +1965,9 @@ async function analyzeRoute(c) {
|
|
|
1771
1965
|
violated_layers: JSON.stringify(mossResult.violated_layers),
|
|
1772
1966
|
reason: mossResult.reason,
|
|
1773
1967
|
confidence: mossResult.confidence,
|
|
1774
|
-
analysis_duration_ms: performance.now() - t0
|
|
1968
|
+
analysis_duration_ms: performance.now() - t0,
|
|
1969
|
+
// 结构化攻击链图(组合式拦截才有;前端弹窗画图用)
|
|
1970
|
+
violation_graph: JSON.stringify(mossResult.violation_graph || null)
|
|
1775
1971
|
});
|
|
1776
1972
|
setCache(key, mossResult);
|
|
1777
1973
|
if (isMultiSessionEnabled()) {
|
|
@@ -1795,6 +1991,7 @@ async function analyzeRoute(c) {
|
|
|
1795
1991
|
violated_layers: mossResult.violated_layers,
|
|
1796
1992
|
confidence: mossResult.confidence,
|
|
1797
1993
|
analysis_duration_ms: Math.round(durationMs * 10) / 10,
|
|
1994
|
+
violation_graph: mossResult.violation_graph ?? null,
|
|
1798
1995
|
...storageOversizeInfo(),
|
|
1799
1996
|
collected_context: {
|
|
1800
1997
|
os: context.os,
|
|
@@ -2043,9 +2240,9 @@ async function dashboardRoute(c) {
|
|
|
2043
2240
|
}
|
|
2044
2241
|
}
|
|
2045
2242
|
const { readFileSync: readFileSync3, existsSync: existsSync3 } = await import("fs");
|
|
2046
|
-
const { resolve, dirname:
|
|
2243
|
+
const { resolve, dirname: dirname4 } = await import("path");
|
|
2047
2244
|
const { fileURLToPath } = await import("url");
|
|
2048
|
-
const __dirname =
|
|
2245
|
+
const __dirname = dirname4(fileURLToPath(importMetaUrl));
|
|
2049
2246
|
const paths = [
|
|
2050
2247
|
resolve(__dirname, "dashboard.html"),
|
|
2051
2248
|
resolve(__dirname, "../dashboard.html"),
|