@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/v1/cli.js CHANGED
@@ -54,6 +54,8 @@ __export(security_exports, {
54
54
  agentInflight: () => agentInflight,
55
55
  agentTokens: () => agentTokens,
56
56
  allowedOrigins: () => allowedOrigins,
57
+ archiveKeepRecords: () => archiveKeepRecords,
58
+ archiveThresholdRecords: () => archiveThresholdRecords,
57
59
  authConfigError: () => authConfigError,
58
60
  authEnabled: () => authEnabled,
59
61
  authMode: () => authMode,
@@ -66,6 +68,8 @@ __export(security_exports, {
66
68
  checkToken: () => checkToken,
67
69
  cookiePathAllowed: () => cookiePathAllowed,
68
70
  describe: () => describe,
71
+ diskDegradePercent: () => diskDegradePercent,
72
+ diskWarnPercent: () => diskWarnPercent,
69
73
  ensureAutoToken: () => ensureAutoToken,
70
74
  ensurePrivateDir: () => ensurePrivateDir,
71
75
  failFastOnAuthError: () => failFastOnAuthError,
@@ -78,6 +82,7 @@ __export(security_exports, {
78
82
  maxConnections: () => maxConnections,
79
83
  maxInflight: () => maxInflight,
80
84
  maxQueue: () => maxQueue,
85
+ maxRecordBytes: () => maxRecordBytes,
81
86
  normalizeOrigin: () => normalizeOrigin,
82
87
  noteQueueReject: () => noteQueueReject,
83
88
  noteSessionAccess: () => noteSessionAccess,
@@ -363,6 +368,21 @@ function bindAgent(principal, agentType) {
363
368
  function maxBodyBytes() {
364
369
  return envInt("AGENT_GATE_MAX_BODY_BYTES", 16 * 1024 * 1024);
365
370
  }
371
+ function maxRecordBytes() {
372
+ return envInt("AGENT_GATE_MAX_RECORD_BYTES", 1024 * 1024);
373
+ }
374
+ function archiveThresholdRecords() {
375
+ return envInt("AGENT_GATE_ARCHIVE_THRESHOLD", 1e5);
376
+ }
377
+ function archiveKeepRecords() {
378
+ return envInt("AGENT_GATE_ARCHIVE_KEEP", 5e4);
379
+ }
380
+ function diskWarnPercent() {
381
+ return envInt("AGENT_GATE_DISK_WARN_PERCENT", 5);
382
+ }
383
+ function diskDegradePercent() {
384
+ return envInt("AGENT_GATE_DISK_DEGRADE_PERCENT", 2);
385
+ }
366
386
  function maxInflight() {
367
387
  return poolLimit("biz");
368
388
  }
@@ -581,6 +601,66 @@ var init_security = __esm({
581
601
  }
582
602
  });
583
603
 
604
+ // src/shared/storage/record-guard.ts
605
+ function clipText(value, limit = maxRecordBytes()) {
606
+ const s = String(value ?? "");
607
+ if (limit <= 0 || s.length <= limit) return s;
608
+ const head = s.slice(0, limit);
609
+ const digest = (0, import_crypto.createHash)("sha256").update(s).digest("hex").slice(0, 16);
610
+ return `${head}\u2026[truncated ${s.length - limit}B sha256:${digest}]`;
611
+ }
612
+ function diskFreePercent(targetPath) {
613
+ try {
614
+ const st = (0, import_fs.statfsSync)((0, import_path.dirname)(targetPath));
615
+ const total = Number(st.blocks) || 0;
616
+ const avail = Number(st.bavail) || 0;
617
+ if (!total) return 100;
618
+ return avail / total * 100;
619
+ } catch {
620
+ return 100;
621
+ }
622
+ }
623
+ function shouldDegradeWrites(targetPath) {
624
+ const degrade = diskDegradePercent();
625
+ if (degrade <= 0) return false;
626
+ return diskFreePercent(targetPath) < degrade;
627
+ }
628
+ function degradeRecord(rec) {
629
+ const command = String(rec.command ?? "");
630
+ const clipped = command.length > DEGRADE_COMMAND_BYTES ? `${command.slice(0, DEGRADE_COMMAND_BYTES)}\u2026[degraded]` : command;
631
+ return {
632
+ ...rec,
633
+ command: clipped,
634
+ action_detail: String(rec.action_detail ?? "") > "" ? String(rec.action_detail).slice(0, DEGRADE_COMMAND_BYTES) : "",
635
+ description: "",
636
+ reason: String(rec.reason ?? "").slice(0, 256)
637
+ };
638
+ }
639
+ function prepareRecordForStorage(rec, targetPath) {
640
+ let out = {
641
+ ...rec,
642
+ command: clipText(rec.command),
643
+ action_detail: clipText(rec.action_detail),
644
+ reason: clipText(rec.reason, 4096),
645
+ description: clipText(rec.description, 4096)
646
+ };
647
+ const degraded = shouldDegradeWrites(targetPath);
648
+ if (degraded) out = degradeRecord(out);
649
+ return { record: out, degraded };
650
+ }
651
+ var import_crypto, import_fs, import_path, DEGRADE_COMMAND_BYTES;
652
+ var init_record_guard = __esm({
653
+ "src/shared/storage/record-guard.ts"() {
654
+ "use strict";
655
+ init_cjs_shims();
656
+ import_crypto = require("crypto");
657
+ import_fs = require("fs");
658
+ import_path = require("path");
659
+ init_security();
660
+ DEGRADE_COMMAND_BYTES = 512;
661
+ }
662
+ });
663
+
584
664
  // src/shared/storage/json-storage.ts
585
665
  var json_storage_exports = {};
586
666
  __export(json_storage_exports, {
@@ -591,17 +671,18 @@ function nowISO() {
591
671
  }
592
672
  function getDefaultDataDir() {
593
673
  const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
594
- const xdgData = process.env.XDG_DATA_HOME || (0, import_path.join)(home, ".local", "share");
595
- return (0, import_path.join)(xdgData, "agent_gate", "storage");
674
+ const xdgData = process.env.XDG_DATA_HOME || (0, import_path2.join)(home, ".local", "share");
675
+ return (0, import_path2.join)(xdgData, "agent_gate", "storage");
596
676
  }
597
- var import_fs, import_path, JsonStorage;
677
+ var import_fs2, import_path2, JsonStorage;
598
678
  var init_json_storage = __esm({
599
679
  "src/shared/storage/json-storage.ts"() {
600
680
  "use strict";
601
681
  init_cjs_shims();
602
- import_fs = require("fs");
603
- import_path = require("path");
682
+ import_fs2 = require("fs");
683
+ import_path2 = require("path");
604
684
  init_security();
685
+ init_record_guard();
605
686
  JsonStorage = class {
606
687
  dataDir;
607
688
  agentsPath;
@@ -614,11 +695,11 @@ var init_json_storage = __esm({
614
695
  sessionLocks = /* @__PURE__ */ new Map();
615
696
  constructor(dataDir) {
616
697
  this.dataDir = dataDir || process.env.AGENT_GATE_DATA_DIR || getDefaultDataDir();
617
- this.agentsPath = (0, import_path.join)(this.dataDir, "agents.json");
618
- this.sessionsPath = (0, import_path.join)(this.dataDir, "sessions.json");
619
- this.toolCallsDir = (0, import_path.join)(this.dataDir, "tool_calls");
620
- if (!(0, import_fs.existsSync)(this.dataDir)) (0, import_fs.mkdirSync)(this.dataDir, { recursive: true, mode: 448 });
621
- if (!(0, import_fs.existsSync)(this.toolCallsDir)) (0, import_fs.mkdirSync)(this.toolCallsDir, { recursive: true, mode: 448 });
698
+ this.agentsPath = (0, import_path2.join)(this.dataDir, "agents.json");
699
+ this.sessionsPath = (0, import_path2.join)(this.dataDir, "sessions.json");
700
+ this.toolCallsDir = (0, import_path2.join)(this.dataDir, "tool_calls");
701
+ if (!(0, import_fs2.existsSync)(this.dataDir)) (0, import_fs2.mkdirSync)(this.dataDir, { recursive: true, mode: 448 });
702
+ if (!(0, import_fs2.existsSync)(this.toolCallsDir)) (0, import_fs2.mkdirSync)(this.toolCallsDir, { recursive: true, mode: 448 });
622
703
  this.hardenStoragePermissions();
623
704
  this.nextId = this.recoverNextId();
624
705
  }
@@ -630,10 +711,10 @@ var init_json_storage = __esm({
630
711
  const uid = process.getuid?.();
631
712
  const tighten = (p, mode) => {
632
713
  try {
633
- const st = (0, import_fs.statSync)(p);
714
+ const st = (0, import_fs2.statSync)(p);
634
715
  if (st.isSymbolicLink()) return;
635
716
  if (uid !== void 0 && st.uid !== uid) return;
636
- if ((st.mode & 511) !== mode) (0, import_fs.chmodSync)(p, mode);
717
+ if ((st.mode & 511) !== mode) (0, import_fs2.chmodSync)(p, mode);
637
718
  } catch {
638
719
  }
639
720
  };
@@ -642,9 +723,9 @@ var init_json_storage = __esm({
642
723
  tighten(this.agentsPath, 384);
643
724
  tighten(this.sessionsPath, 384);
644
725
  try {
645
- for (const f of (0, import_fs.readdirSync)(this.toolCallsDir)) {
726
+ for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
646
727
  if (f.endsWith(".json") || f.endsWith(".tmp")) {
647
- tighten((0, import_path.join)(this.toolCallsDir, f), 384);
728
+ tighten((0, import_path2.join)(this.toolCallsDir, f), 384);
648
729
  }
649
730
  }
650
731
  } catch {
@@ -654,9 +735,9 @@ var init_json_storage = __esm({
654
735
  recoverNextId() {
655
736
  let maxId = 0;
656
737
  try {
657
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
738
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
658
739
  for (const file of files) {
659
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
740
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
660
741
  for (const r of records) {
661
742
  if (r.id > maxId) maxId = r.id;
662
743
  }
@@ -667,19 +748,19 @@ var init_json_storage = __esm({
667
748
  }
668
749
  readJsonFile(path, fallback) {
669
750
  try {
670
- if (!(0, import_fs.existsSync)(path)) return fallback;
671
- return JSON.parse((0, import_fs.readFileSync)(path, "utf-8"));
751
+ if (!(0, import_fs2.existsSync)(path)) return fallback;
752
+ return JSON.parse((0, import_fs2.readFileSync)(path, "utf-8"));
672
753
  } catch {
673
754
  return fallback;
674
755
  }
675
756
  }
676
757
  /** 原子写入:先写 .tmp 再 rename(rename 保留 tmp 的 0600 权限) */
677
758
  writeJsonFile(path, data) {
678
- const dir = (0, import_path.dirname)(path);
679
- if (!(0, import_fs.existsSync)(dir)) (0, import_fs.mkdirSync)(dir, { recursive: true, mode: 448 });
759
+ const dir = (0, import_path2.dirname)(path);
760
+ if (!(0, import_fs2.existsSync)(dir)) (0, import_fs2.mkdirSync)(dir, { recursive: true, mode: 448 });
680
761
  const tmp = path + ".tmp";
681
- (0, import_fs.writeFileSync)(tmp, JSON.stringify(data, null, 2), { encoding: "utf-8", mode: 384 });
682
- (0, import_fs.renameSync)(tmp, path);
762
+ (0, import_fs2.writeFileSync)(tmp, JSON.stringify(data, null, 2), { encoding: "utf-8", mode: 384 });
763
+ (0, import_fs2.renameSync)(tmp, path);
683
764
  }
684
765
  loadAgents() {
685
766
  const raw = this.readJsonFile(this.agentsPath, {});
@@ -697,12 +778,12 @@ var init_json_storage = __esm({
697
778
  }
698
779
  loadToolCalls(sessionId) {
699
780
  return this.readJsonFile(
700
- (0, import_path.join)(this.toolCallsDir, `${sessionId}.json`),
781
+ (0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`),
701
782
  []
702
783
  );
703
784
  }
704
785
  saveToolCalls(sessionId, records) {
705
- this.writeJsonFile((0, import_path.join)(this.toolCallsDir, `${sessionId}.json`), records);
786
+ this.writeJsonFile((0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`), records);
706
787
  }
707
788
  getOrCreateAgent(agentType, hostname, version2 = "") {
708
789
  const agents = this.loadAgents();
@@ -747,6 +828,10 @@ var init_json_storage = __esm({
747
828
  return Math.max(...records.map((r) => r.seq)) + 1;
748
829
  }
749
830
  insertToolCall(record) {
831
+ record = prepareRecordForStorage(
832
+ record,
833
+ this.dataDir
834
+ ).record;
750
835
  const sessionId = record.session_id;
751
836
  const prev = this.sessionLocks.get(sessionId) ?? Promise.resolve();
752
837
  const next = prev.then(() => {
@@ -784,9 +869,9 @@ var init_json_storage = __esm({
784
869
  allRecords = records.map((r) => this.enrichRecord(r, sessions, agents));
785
870
  } else {
786
871
  try {
787
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
872
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
788
873
  for (const file of files) {
789
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
874
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
790
875
  allRecords.push(...records.map((r) => this.enrichRecord(r, sessions, agents)));
791
876
  }
792
877
  } catch {
@@ -820,9 +905,9 @@ var init_json_storage = __esm({
820
905
  const agents = this.loadAgents();
821
906
  let allRecords = [];
822
907
  try {
823
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
908
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
824
909
  for (const file of files) {
825
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
910
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
826
911
  allRecords.push(...records);
827
912
  }
828
913
  } catch {
@@ -861,12 +946,12 @@ var init_json_storage = __esm({
861
946
  let jsonFiles = 0;
862
947
  let jsonBytes = 0;
863
948
  try {
864
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
949
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
865
950
  for (const file of files) {
866
- const stat = (0, import_fs.statSync)((0, import_path.join)(this.toolCallsDir, file));
951
+ const stat = (0, import_fs2.statSync)((0, import_path2.join)(this.toolCallsDir, file));
867
952
  jsonFiles++;
868
953
  jsonBytes += stat.size;
869
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
954
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
870
955
  toolCalls += records.length;
871
956
  }
872
957
  } catch {
@@ -876,9 +961,9 @@ var init_json_storage = __esm({
876
961
  clearToolCalls() {
877
962
  let removed = 0;
878
963
  try {
879
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
964
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
880
965
  for (const file of files) {
881
- (0, import_fs.unlinkSync)((0, import_path.join)(this.toolCallsDir, file));
966
+ (0, import_fs2.unlinkSync)((0, import_path2.join)(this.toolCallsDir, file));
882
967
  removed++;
883
968
  }
884
969
  } catch {
@@ -896,6 +981,49 @@ var init_json_storage = __esm({
896
981
  hostname: agent?.hostname || ""
897
982
  };
898
983
  }
984
+ /**
985
+ * 归档最老的记录:JSON 模式下每个 session 一个文件,这里把所有 session 的记录
986
+ * 汇总排序,最老的 (total - keep) 条写进 `<dataDir>/archive/*.jsonl.gz`,
987
+ * 再从各自的 session 文件里移除。数据不丢,只是不在主目录了。
988
+ */
989
+ archiveOldRecords(keep) {
990
+ if (keep <= 0 || !(0, import_fs2.existsSync)(this.toolCallsDir)) return { archived: 0, file: "" };
991
+ const bySession = /* @__PURE__ */ new Map();
992
+ let total = 0;
993
+ for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
994
+ if (!f.endsWith(".json")) continue;
995
+ const sid = f.slice(0, -".json".length);
996
+ const recs = this.loadToolCalls(sid);
997
+ if (recs.length) {
998
+ bySession.set(sid, recs);
999
+ total += recs.length;
1000
+ }
1001
+ }
1002
+ if (total <= keep) return { archived: 0, file: "" };
1003
+ const flat = [];
1004
+ for (const [sid, recs] of bySession) for (const rec of recs) flat.push({ sid, rec });
1005
+ flat.sort((a, b) => String(a.rec.called_at).localeCompare(String(b.rec.called_at)));
1006
+ const archiveCount = total - keep;
1007
+ const toArchive = flat.slice(0, archiveCount);
1008
+ const archiveIds = new Set(toArchive.map((x) => String(x.rec.id)));
1009
+ const { mkdirSync: mkdirSync4, writeFileSync: writeFileSync3 } = require("fs");
1010
+ const { join: join4 } = require("path");
1011
+ const { gzipSync } = require("zlib");
1012
+ const archiveDir = join4(this.dataDir, "archive");
1013
+ if (!(0, import_fs2.existsSync)(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
1014
+ const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
1015
+ const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
1016
+ const payload = toArchive.map((x) => JSON.stringify(x.rec)).join("\n") + "\n";
1017
+ writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
1018
+ for (const [sid, recs] of bySession) {
1019
+ const remain = recs.filter((r) => !archiveIds.has(String(r.id)));
1020
+ if (remain.length !== recs.length) this.saveToolCalls(sid, remain);
1021
+ }
1022
+ return { archived: toArchive.length, file };
1023
+ }
1024
+ /** JSON 模式没有需要回收的数据库页,归档已把文件挪走。 */
1025
+ vacuum() {
1026
+ }
899
1027
  getAgentsList(agentType) {
900
1028
  const agents = this.loadAgents();
901
1029
  const sessions = this.loadSessions();
@@ -930,9 +1058,9 @@ var init_json_storage = __esm({
930
1058
  getTimeline(limit = 100) {
931
1059
  const buckets = /* @__PURE__ */ new Map();
932
1060
  try {
933
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
1061
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
934
1062
  for (const file of files) {
935
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
1063
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
936
1064
  for (const r of records) {
937
1065
  const minute = r.called_at.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, "$1T$2");
938
1066
  const bucket = buckets.get(minute) || { allow: 0, deny: 0 };
@@ -963,16 +1091,17 @@ __export(sqlite_storage_exports, {
963
1091
  });
964
1092
  function getDefaultDbPath() {
965
1093
  const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
966
- const xdgData = process.env.XDG_DATA_HOME || (0, import_path2.join)(home, ".local", "share");
967
- return (0, import_path2.join)(xdgData, "agent_gate", "agent_gate_ts.db");
1094
+ const xdgData = process.env.XDG_DATA_HOME || (0, import_path3.join)(home, ".local", "share");
1095
+ return (0, import_path3.join)(xdgData, "agent_gate", "agent_gate_ts.db");
968
1096
  }
969
- var import_fs2, import_path2, SCHEMA, SqliteStorage;
1097
+ var import_fs3, import_path3, SCHEMA, SqliteStorage;
970
1098
  var init_sqlite_storage = __esm({
971
1099
  "src/shared/storage/sqlite-storage.ts"() {
972
1100
  "use strict";
973
1101
  init_cjs_shims();
974
- import_fs2 = require("fs");
975
- import_path2 = require("path");
1102
+ import_fs3 = require("fs");
1103
+ import_path3 = require("path");
1104
+ init_record_guard();
976
1105
  SCHEMA = `
977
1106
  CREATE TABLE IF NOT EXISTS agents (
978
1107
  id TEXT PRIMARY KEY,
@@ -1012,6 +1141,7 @@ CREATE TABLE IF NOT EXISTS tool_calls (
1012
1141
  reason TEXT DEFAULT '',
1013
1142
  confidence INTEGER DEFAULT 0,
1014
1143
  analysis_duration_ms REAL DEFAULT 0,
1144
+ violation_graph TEXT DEFAULT '{}',
1015
1145
  called_at TEXT DEFAULT (datetime('now')),
1016
1146
  UNIQUE(session_id, seq)
1017
1147
  );
@@ -1032,12 +1162,23 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1032
1162
  );
1033
1163
  }
1034
1164
  const path = dbPath || getDefaultDbPath();
1035
- const dir = (0, import_path2.dirname)(path);
1036
- if (!(0, import_fs2.existsSync)(dir)) (0, import_fs2.mkdirSync)(dir, { recursive: true });
1165
+ const dir = (0, import_path3.dirname)(path);
1166
+ if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
1037
1167
  this.dbPath = path;
1038
1168
  this.db = new Database(path);
1039
1169
  this.db.pragma("journal_mode = WAL");
1040
1170
  this.db.exec(SCHEMA);
1171
+ this.ensureColumn("tool_calls", "violation_graph", "violation_graph TEXT DEFAULT '{}'");
1172
+ }
1173
+ /** 幂等补列(旧数据库平滑升级,失败不阻断启动)。 */
1174
+ ensureColumn(table, column, ddl) {
1175
+ try {
1176
+ const cols = this.db.prepare(`PRAGMA table_info(${table})`).all();
1177
+ if (!cols.some((c) => c.name === column)) {
1178
+ this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
1179
+ }
1180
+ } catch {
1181
+ }
1041
1182
  }
1042
1183
  static isAvailable() {
1043
1184
  try {
@@ -1067,6 +1208,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1067
1208
  return row.next;
1068
1209
  }
1069
1210
  insertToolCall(record) {
1211
+ record = prepareRecordForStorage(
1212
+ record,
1213
+ this.dbPath
1214
+ ).record;
1070
1215
  const result = this.db.transaction((rec) => {
1071
1216
  const row = this.db.prepare(
1072
1217
  "SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
@@ -1075,8 +1220,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1075
1220
  return this.db.prepare(`
1076
1221
  INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
1077
1222
  action_type, action_detail, decision, risk_level, risk_type, violated_layers,
1078
- reason, confidence, analysis_duration_ms)
1079
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1223
+ reason, confidence, analysis_duration_ms, violation_graph)
1224
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1080
1225
  `).run(
1081
1226
  rec.session_id,
1082
1227
  seq,
@@ -1092,7 +1237,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1092
1237
  rec.violated_layers,
1093
1238
  rec.reason,
1094
1239
  rec.confidence,
1095
- rec.analysis_duration_ms
1240
+ rec.analysis_duration_ms,
1241
+ rec.violation_graph || "{}"
1096
1242
  );
1097
1243
  })(record);
1098
1244
  return Number(result.lastInsertRowid);
@@ -1207,6 +1353,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1207
1353
  })();
1208
1354
  return result;
1209
1355
  }
1356
+ /**
1357
+ * 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
1358
+ *
1359
+ * 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
1360
+ * 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
1361
+ * 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
1362
+ */
1363
+ archiveOldRecords(keep) {
1364
+ if (keep <= 0) return { archived: 0, file: "" };
1365
+ const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
1366
+ if (total <= keep) return { archived: 0, file: "" };
1367
+ const cutoff = this.db.prepare(
1368
+ "SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
1369
+ ).get(total - keep - 1);
1370
+ if (!cutoff) return { archived: 0, file: "" };
1371
+ const rows = this.db.prepare(
1372
+ "SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
1373
+ ).all(cutoff.id);
1374
+ if (rows.length === 0) return { archived: 0, file: "" };
1375
+ const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
1376
+ const { dirname: dirname4, join: join4 } = require("path");
1377
+ const { gzipSync } = require("zlib");
1378
+ const archiveDir = join4(dirname4(this.dbPath), "archive");
1379
+ if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
1380
+ const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
1381
+ const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
1382
+ const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
1383
+ writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
1384
+ const deleted = this.db.transaction(() => {
1385
+ const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
1386
+ this.db.prepare(`
1387
+ UPDATE sessions SET tool_call_count = (
1388
+ SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
1389
+ )
1390
+ `).run();
1391
+ return Number(res.changes);
1392
+ })();
1393
+ return { archived: deleted, file };
1394
+ }
1395
+ /** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
1396
+ vacuum() {
1397
+ this.db.exec("VACUUM");
1398
+ }
1210
1399
  getAgentsList(agentType) {
1211
1400
  const where = agentType ? "WHERE a.agent_type = ?" : "";
1212
1401
  return this.db.prepare(`
@@ -1298,12 +1487,12 @@ function getDb(dbPath) {
1298
1487
  try {
1299
1488
  const Database = require("better-sqlite3");
1300
1489
  const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
1301
- const { join: join4, dirname: dirname3 } = require("path");
1490
+ const { join: join4, dirname: dirname4 } = require("path");
1302
1491
  const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
1303
1492
  const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
1304
1493
  const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
1305
1494
  const path = dbPath || defaultPath;
1306
- const dir = dirname3(path);
1495
+ const dir = dirname4(path);
1307
1496
  if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
1308
1497
  rawDb = new Database(path);
1309
1498
  rawDb.pragma("journal_mode = WAL");
@@ -1359,15 +1548,29 @@ function sessionHistoryLimit() {
1359
1548
  const v = Number.isFinite(n) ? n : DEFAULT_MAX_SESSIONS;
1360
1549
  return Math.max(1, Math.min(v, 50));
1361
1550
  }
1362
- function buildSessionHistory(queryToolCalls2, sessionId) {
1551
+ function buildSessionHistory(queryToolCalls2, sessionId, listAgentTypes) {
1363
1552
  if (!isMultiSessionEnabled()) return [];
1364
1553
  const maxSessions = sessionHistoryLimit();
1365
1554
  try {
1366
1555
  const scanLimit = Math.max(DEFAULT_HISTORY_LIMIT, maxSessions * DEFAULT_ACTIONS_PER_SESSION);
1367
- const recent = queryToolCalls2({ limit: scanLimit });
1556
+ const agentTypes = listAgentTypes?.() ?? [];
1557
+ let recent;
1558
+ if (agentTypes.length > 1) {
1559
+ const perAgent = Math.max(
1560
+ DEFAULT_ACTIONS_PER_SESSION,
1561
+ Math.floor(DEFAULT_HISTORY_LIMIT / agentTypes.length)
1562
+ );
1563
+ recent = agentTypes.flatMap((a) => queryToolCalls2({ limit: perAgent, agentType: a }));
1564
+ } else {
1565
+ recent = queryToolCalls2({ limit: scanLimit });
1566
+ }
1368
1567
  const bySession = /* @__PURE__ */ new Map();
1568
+ const sessionAgent = /* @__PURE__ */ new Map();
1369
1569
  for (const tc of recent) {
1370
1570
  if (tc.session_id === sessionId) continue;
1571
+ if (tc.agent_type && !sessionAgent.has(tc.session_id)) {
1572
+ sessionAgent.set(tc.session_id, tc.agent_type);
1573
+ }
1371
1574
  const list = bySession.get(tc.session_id) || [];
1372
1575
  list.unshift({
1373
1576
  action_type: tc.action_type,
@@ -1378,6 +1581,7 @@ function buildSessionHistory(queryToolCalls2, sessionId) {
1378
1581
  }
1379
1582
  return [...bySession.entries()].slice(0, maxSessions).map(([sid, actions]) => ({
1380
1583
  session_id: sid,
1584
+ ...sessionAgent.get(sid) ? { agent_id: sessionAgent.get(sid) } : {},
1381
1585
  action_history: actions.slice(0, DEFAULT_ACTIONS_PER_SESSION)
1382
1586
  }));
1383
1587
  } catch {
@@ -1519,7 +1723,7 @@ var init_version = __esm({
1519
1723
  "src/version.ts"() {
1520
1724
  "use strict";
1521
1725
  init_cjs_shims();
1522
- _version = true ? "0.9.0" : (() => {
1726
+ _version = true ? "0.9.2" : (() => {
1523
1727
  try {
1524
1728
  const require2 = createRequire(importMetaUrl);
1525
1729
  const pkg = require2("../package.json");
@@ -1586,6 +1790,8 @@ var init_models = __esm({
1586
1790
  violated_layers: import_zod.z.array(import_zod.z.string()).default([]),
1587
1791
  confidence: import_zod.z.number().int().min(0).max(100).default(100),
1588
1792
  analysis_duration_ms: import_zod.z.number().default(0),
1793
+ // 结构化攻击链图(组合式拦截时非空;前端弹窗画图用)
1794
+ violation_graph: import_zod.z.any().nullable().optional(),
1589
1795
  oversize: import_zod.z.boolean().optional().describe("\u672C\u5730\u5B58\u50A8\u662F\u5426\u8D85\u8FC7\u6E05\u7406\u9608\u503C\uFF08agg \u7528\uFF09"),
1590
1796
  thresholdMB: import_zod.z.number().optional().describe("\u5B58\u50A8\u6E05\u7406\u9608\u503C MB\uFF08agg \u7528\uFF09"),
1591
1797
  collected_context: import_zod.z.object({
@@ -1824,6 +2030,12 @@ function getStorageStats() {
1824
2030
  function clearToolCalls() {
1825
2031
  return store().clearToolCalls();
1826
2032
  }
2033
+ function archiveOldRecords(keep) {
2034
+ return store().archiveOldRecords(keep);
2035
+ }
2036
+ function vacuumStore() {
2037
+ store().vacuum();
2038
+ }
1827
2039
  function getAgentsList(agentType) {
1828
2040
  return store().getAgentsList(agentType);
1829
2041
  }
@@ -1902,7 +2114,9 @@ async function analyzeRoute(c) {
1902
2114
  if (isMultiSessionEnabled()) {
1903
2115
  input.session_history = buildSessionHistory(
1904
2116
  (params) => queryToolCalls(params),
1905
- input.session_id
2117
+ input.session_id,
2118
+ // 多 agent 时按 agent 分组取历史,避免高频 agent 把别人的动作挤出窗口
2119
+ () => getAgentsList().map((a) => a.agent_type)
1906
2120
  );
1907
2121
  }
1908
2122
  const mossRequest = normalize(input, context);
@@ -1928,7 +2142,9 @@ async function analyzeRoute(c) {
1928
2142
  violated_layers: JSON.stringify(mossResult.violated_layers),
1929
2143
  reason: mossResult.reason,
1930
2144
  confidence: mossResult.confidence,
1931
- analysis_duration_ms: performance.now() - t0
2145
+ analysis_duration_ms: performance.now() - t0,
2146
+ // 结构化攻击链图(组合式拦截才有;前端弹窗画图用)
2147
+ violation_graph: JSON.stringify(mossResult.violation_graph || null)
1932
2148
  });
1933
2149
  setCache(key, mossResult);
1934
2150
  if (isMultiSessionEnabled()) {
@@ -1952,6 +2168,7 @@ async function analyzeRoute(c) {
1952
2168
  violated_layers: mossResult.violated_layers,
1953
2169
  confidence: mossResult.confidence,
1954
2170
  analysis_duration_ms: Math.round(durationMs * 10) / 10,
2171
+ violation_graph: mossResult.violation_graph ?? null,
1955
2172
  ...storageOversizeInfo(),
1956
2173
  collected_context: {
1957
2174
  os: context.os,
@@ -2190,9 +2407,9 @@ async function dashboardRoute(c) {
2190
2407
  }
2191
2408
  }
2192
2409
  const { readFileSync: readFileSync3, existsSync: existsSync3 } = await import("fs");
2193
- const { resolve, dirname: dirname3 } = await import("path");
2410
+ const { resolve, dirname: dirname4 } = await import("path");
2194
2411
  const { fileURLToPath } = await import("url");
2195
- const __dirname = dirname3(fileURLToPath(importMetaUrl));
2412
+ const __dirname = dirname4(fileURLToPath(importMetaUrl));
2196
2413
  const paths = [
2197
2414
  resolve(__dirname, "dashboard.html"),
2198
2415
  resolve(__dirname, "../dashboard.html"),
@@ -2247,6 +2464,73 @@ p{margin:8px 0;color:#555}</style></head><body>
2247
2464
  }
2248
2465
  });
2249
2466
 
2467
+ // src/shared/storage/maintenance.ts
2468
+ var maintenance_exports = {};
2469
+ __export(maintenance_exports, {
2470
+ runStorageMaintenance: () => runStorageMaintenance,
2471
+ startStorageMaintenance: () => startStorageMaintenance,
2472
+ stopStorageMaintenance: () => stopStorageMaintenance
2473
+ });
2474
+ async function runStorageMaintenance(agentId = "default") {
2475
+ if (running) return { archived: 0, file: "", skipped: "running" };
2476
+ running = true;
2477
+ try {
2478
+ const threshold = archiveThresholdRecords();
2479
+ const keep = archiveKeepRecords();
2480
+ if (threshold <= 0 || keep <= 0) return { archived: 0, file: "", skipped: "disabled" };
2481
+ const stats = getStorageStats();
2482
+ if (stats.toolCalls < threshold) return { archived: 0, file: "", skipped: "below-threshold" };
2483
+ try {
2484
+ await callAgentMossReview({ agentId, dbPath: getAgentGateDbPath() });
2485
+ } catch (err) {
2486
+ console.error(`[AgentGate] \u5F52\u6863\u524D\u56DE\u987E\u5931\u8D25\uFF08\u7EE7\u7EED\u5F52\u6863\uFF09: ${String(err?.message ?? err)}`);
2487
+ }
2488
+ const res = archiveOldRecords(keep);
2489
+ if (res.archived <= 0) return { archived: 0, file: "", skipped: "nothing-to-archive" };
2490
+ try {
2491
+ vacuumStore();
2492
+ } catch (err) {
2493
+ console.error(`[AgentGate] VACUUM \u5931\u8D25: ${String(err?.message ?? err)}`);
2494
+ }
2495
+ console.error(
2496
+ `[AgentGate] \u5B58\u50A8\u5F52\u6863\u5B8C\u6210: ${res.archived} \u6761 \u2192 ${res.file}\uFF08\u4E3B\u5E93\u4FDD\u7559 ${keep} \u6761\uFF09`
2497
+ );
2498
+ return { archived: res.archived, file: res.file, skipped: "" };
2499
+ } catch (err) {
2500
+ console.error(`[AgentGate] \u5B58\u50A8\u7EF4\u62A4\u5931\u8D25: ${String(err?.message ?? err)}`);
2501
+ return { archived: 0, file: "", skipped: "error" };
2502
+ } finally {
2503
+ running = false;
2504
+ }
2505
+ }
2506
+ function startStorageMaintenance(agentId = "default", intervalMs = 6 * 60 * 60 * 1e3) {
2507
+ if (timer) return;
2508
+ void runStorageMaintenance(agentId);
2509
+ timer = setInterval(() => {
2510
+ void runStorageMaintenance(agentId);
2511
+ }, intervalMs);
2512
+ timer.unref?.();
2513
+ }
2514
+ function stopStorageMaintenance() {
2515
+ if (timer) {
2516
+ clearInterval(timer);
2517
+ timer = null;
2518
+ }
2519
+ }
2520
+ var running, timer;
2521
+ var init_maintenance = __esm({
2522
+ "src/shared/storage/maintenance.ts"() {
2523
+ "use strict";
2524
+ init_cjs_shims();
2525
+ init_repository();
2526
+ init_security();
2527
+ init_moss_client();
2528
+ init_db();
2529
+ running = false;
2530
+ timer = null;
2531
+ }
2532
+ });
2533
+
2250
2534
  // src/v1/cli.ts
2251
2535
  init_cjs_shims();
2252
2536
  async function main() {
@@ -2306,6 +2590,12 @@ async function main() {
2306
2590
  serve({ fetch: app.fetch, hostname: host, port }, (info) => {
2307
2591
  console.log(`Listening on http://${host}:${port}`);
2308
2592
  });
2593
+ try {
2594
+ const { startStorageMaintenance: startStorageMaintenance2 } = await Promise.resolve().then(() => (init_maintenance(), maintenance_exports));
2595
+ startStorageMaintenance2("default");
2596
+ } catch (err) {
2597
+ console.error(`[AgentGate] \u5B58\u50A8\u7EF4\u62A4\u542F\u52A8\u5931\u8D25: ${String(err?.message ?? err)}`);
2598
+ }
2309
2599
  }
2310
2600
  main().catch((err) => {
2311
2601
  console.error("Fatal error:", err);