@kenhkl/agent-gate 0.9.0 → 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/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,
@@ -1032,8 +1161,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1032
1161
  );
1033
1162
  }
1034
1163
  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 });
1164
+ const dir = (0, import_path3.dirname)(path);
1165
+ if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
1037
1166
  this.dbPath = path;
1038
1167
  this.db = new Database(path);
1039
1168
  this.db.pragma("journal_mode = WAL");
@@ -1067,6 +1196,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1067
1196
  return row.next;
1068
1197
  }
1069
1198
  insertToolCall(record) {
1199
+ record = prepareRecordForStorage(
1200
+ record,
1201
+ this.dbPath
1202
+ ).record;
1070
1203
  const result = this.db.transaction((rec) => {
1071
1204
  const row = this.db.prepare(
1072
1205
  "SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
@@ -1207,6 +1340,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1207
1340
  })();
1208
1341
  return result;
1209
1342
  }
1343
+ /**
1344
+ * 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
1345
+ *
1346
+ * 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
1347
+ * 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
1348
+ * 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
1349
+ */
1350
+ archiveOldRecords(keep) {
1351
+ if (keep <= 0) return { archived: 0, file: "" };
1352
+ const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
1353
+ if (total <= keep) return { archived: 0, file: "" };
1354
+ const cutoff = this.db.prepare(
1355
+ "SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
1356
+ ).get(total - keep - 1);
1357
+ if (!cutoff) return { archived: 0, file: "" };
1358
+ const rows = this.db.prepare(
1359
+ "SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
1360
+ ).all(cutoff.id);
1361
+ if (rows.length === 0) return { archived: 0, file: "" };
1362
+ const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
1363
+ const { dirname: dirname4, join: join4 } = require("path");
1364
+ const { gzipSync } = require("zlib");
1365
+ const archiveDir = join4(dirname4(this.dbPath), "archive");
1366
+ if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
1367
+ const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
1368
+ const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
1369
+ const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
1370
+ writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
1371
+ const deleted = this.db.transaction(() => {
1372
+ const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
1373
+ this.db.prepare(`
1374
+ UPDATE sessions SET tool_call_count = (
1375
+ SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
1376
+ )
1377
+ `).run();
1378
+ return Number(res.changes);
1379
+ })();
1380
+ return { archived: deleted, file };
1381
+ }
1382
+ /** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
1383
+ vacuum() {
1384
+ this.db.exec("VACUUM");
1385
+ }
1210
1386
  getAgentsList(agentType) {
1211
1387
  const where = agentType ? "WHERE a.agent_type = ?" : "";
1212
1388
  return this.db.prepare(`
@@ -1298,12 +1474,12 @@ function getDb(dbPath) {
1298
1474
  try {
1299
1475
  const Database = require("better-sqlite3");
1300
1476
  const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
1301
- const { join: join4, dirname: dirname3 } = require("path");
1477
+ const { join: join4, dirname: dirname4 } = require("path");
1302
1478
  const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
1303
1479
  const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
1304
1480
  const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
1305
1481
  const path = dbPath || defaultPath;
1306
- const dir = dirname3(path);
1482
+ const dir = dirname4(path);
1307
1483
  if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
1308
1484
  rawDb = new Database(path);
1309
1485
  rawDb.pragma("journal_mode = WAL");
@@ -1359,15 +1535,29 @@ function sessionHistoryLimit() {
1359
1535
  const v = Number.isFinite(n) ? n : DEFAULT_MAX_SESSIONS;
1360
1536
  return Math.max(1, Math.min(v, 50));
1361
1537
  }
1362
- function buildSessionHistory(queryToolCalls2, sessionId) {
1538
+ function buildSessionHistory(queryToolCalls2, sessionId, listAgentTypes) {
1363
1539
  if (!isMultiSessionEnabled()) return [];
1364
1540
  const maxSessions = sessionHistoryLimit();
1365
1541
  try {
1366
1542
  const scanLimit = Math.max(DEFAULT_HISTORY_LIMIT, maxSessions * DEFAULT_ACTIONS_PER_SESSION);
1367
- const recent = queryToolCalls2({ limit: scanLimit });
1543
+ const agentTypes = listAgentTypes?.() ?? [];
1544
+ let recent;
1545
+ if (agentTypes.length > 1) {
1546
+ const perAgent = Math.max(
1547
+ DEFAULT_ACTIONS_PER_SESSION,
1548
+ Math.floor(DEFAULT_HISTORY_LIMIT / agentTypes.length)
1549
+ );
1550
+ recent = agentTypes.flatMap((a) => queryToolCalls2({ limit: perAgent, agentType: a }));
1551
+ } else {
1552
+ recent = queryToolCalls2({ limit: scanLimit });
1553
+ }
1368
1554
  const bySession = /* @__PURE__ */ new Map();
1555
+ const sessionAgent = /* @__PURE__ */ new Map();
1369
1556
  for (const tc of recent) {
1370
1557
  if (tc.session_id === sessionId) continue;
1558
+ if (tc.agent_type && !sessionAgent.has(tc.session_id)) {
1559
+ sessionAgent.set(tc.session_id, tc.agent_type);
1560
+ }
1371
1561
  const list = bySession.get(tc.session_id) || [];
1372
1562
  list.unshift({
1373
1563
  action_type: tc.action_type,
@@ -1378,6 +1568,7 @@ function buildSessionHistory(queryToolCalls2, sessionId) {
1378
1568
  }
1379
1569
  return [...bySession.entries()].slice(0, maxSessions).map(([sid, actions]) => ({
1380
1570
  session_id: sid,
1571
+ ...sessionAgent.get(sid) ? { agent_id: sessionAgent.get(sid) } : {},
1381
1572
  action_history: actions.slice(0, DEFAULT_ACTIONS_PER_SESSION)
1382
1573
  }));
1383
1574
  } catch {
@@ -1519,7 +1710,7 @@ var init_version = __esm({
1519
1710
  "src/version.ts"() {
1520
1711
  "use strict";
1521
1712
  init_cjs_shims();
1522
- _version = true ? "0.9.0" : (() => {
1713
+ _version = true ? "0.9.1" : (() => {
1523
1714
  try {
1524
1715
  const require2 = createRequire(importMetaUrl);
1525
1716
  const pkg = require2("../package.json");
@@ -1824,6 +2015,12 @@ function getStorageStats() {
1824
2015
  function clearToolCalls() {
1825
2016
  return store().clearToolCalls();
1826
2017
  }
2018
+ function archiveOldRecords(keep) {
2019
+ return store().archiveOldRecords(keep);
2020
+ }
2021
+ function vacuumStore() {
2022
+ store().vacuum();
2023
+ }
1827
2024
  function getAgentsList(agentType) {
1828
2025
  return store().getAgentsList(agentType);
1829
2026
  }
@@ -1902,7 +2099,9 @@ async function analyzeRoute(c) {
1902
2099
  if (isMultiSessionEnabled()) {
1903
2100
  input.session_history = buildSessionHistory(
1904
2101
  (params) => queryToolCalls(params),
1905
- input.session_id
2102
+ input.session_id,
2103
+ // 多 agent 时按 agent 分组取历史,避免高频 agent 把别人的动作挤出窗口
2104
+ () => getAgentsList().map((a) => a.agent_type)
1906
2105
  );
1907
2106
  }
1908
2107
  const mossRequest = normalize(input, context);
@@ -2190,9 +2389,9 @@ async function dashboardRoute(c) {
2190
2389
  }
2191
2390
  }
2192
2391
  const { readFileSync: readFileSync3, existsSync: existsSync3 } = await import("fs");
2193
- const { resolve, dirname: dirname3 } = await import("path");
2392
+ const { resolve, dirname: dirname4 } = await import("path");
2194
2393
  const { fileURLToPath } = await import("url");
2195
- const __dirname = dirname3(fileURLToPath(importMetaUrl));
2394
+ const __dirname = dirname4(fileURLToPath(importMetaUrl));
2196
2395
  const paths = [
2197
2396
  resolve(__dirname, "dashboard.html"),
2198
2397
  resolve(__dirname, "../dashboard.html"),
@@ -2247,6 +2446,73 @@ p{margin:8px 0;color:#555}</style></head><body>
2247
2446
  }
2248
2447
  });
2249
2448
 
2449
+ // src/shared/storage/maintenance.ts
2450
+ var maintenance_exports = {};
2451
+ __export(maintenance_exports, {
2452
+ runStorageMaintenance: () => runStorageMaintenance,
2453
+ startStorageMaintenance: () => startStorageMaintenance,
2454
+ stopStorageMaintenance: () => stopStorageMaintenance
2455
+ });
2456
+ async function runStorageMaintenance(agentId = "default") {
2457
+ if (running) return { archived: 0, file: "", skipped: "running" };
2458
+ running = true;
2459
+ try {
2460
+ const threshold = archiveThresholdRecords();
2461
+ const keep = archiveKeepRecords();
2462
+ if (threshold <= 0 || keep <= 0) return { archived: 0, file: "", skipped: "disabled" };
2463
+ const stats = getStorageStats();
2464
+ if (stats.toolCalls < threshold) return { archived: 0, file: "", skipped: "below-threshold" };
2465
+ try {
2466
+ await callAgentMossReview({ agentId, dbPath: getAgentGateDbPath() });
2467
+ } catch (err) {
2468
+ console.error(`[AgentGate] \u5F52\u6863\u524D\u56DE\u987E\u5931\u8D25\uFF08\u7EE7\u7EED\u5F52\u6863\uFF09: ${String(err?.message ?? err)}`);
2469
+ }
2470
+ const res = archiveOldRecords(keep);
2471
+ if (res.archived <= 0) return { archived: 0, file: "", skipped: "nothing-to-archive" };
2472
+ try {
2473
+ vacuumStore();
2474
+ } catch (err) {
2475
+ console.error(`[AgentGate] VACUUM \u5931\u8D25: ${String(err?.message ?? err)}`);
2476
+ }
2477
+ console.error(
2478
+ `[AgentGate] \u5B58\u50A8\u5F52\u6863\u5B8C\u6210: ${res.archived} \u6761 \u2192 ${res.file}\uFF08\u4E3B\u5E93\u4FDD\u7559 ${keep} \u6761\uFF09`
2479
+ );
2480
+ return { archived: res.archived, file: res.file, skipped: "" };
2481
+ } catch (err) {
2482
+ console.error(`[AgentGate] \u5B58\u50A8\u7EF4\u62A4\u5931\u8D25: ${String(err?.message ?? err)}`);
2483
+ return { archived: 0, file: "", skipped: "error" };
2484
+ } finally {
2485
+ running = false;
2486
+ }
2487
+ }
2488
+ function startStorageMaintenance(agentId = "default", intervalMs = 6 * 60 * 60 * 1e3) {
2489
+ if (timer) return;
2490
+ void runStorageMaintenance(agentId);
2491
+ timer = setInterval(() => {
2492
+ void runStorageMaintenance(agentId);
2493
+ }, intervalMs);
2494
+ timer.unref?.();
2495
+ }
2496
+ function stopStorageMaintenance() {
2497
+ if (timer) {
2498
+ clearInterval(timer);
2499
+ timer = null;
2500
+ }
2501
+ }
2502
+ var running, timer;
2503
+ var init_maintenance = __esm({
2504
+ "src/shared/storage/maintenance.ts"() {
2505
+ "use strict";
2506
+ init_cjs_shims();
2507
+ init_repository();
2508
+ init_security();
2509
+ init_moss_client();
2510
+ init_db();
2511
+ running = false;
2512
+ timer = null;
2513
+ }
2514
+ });
2515
+
2250
2516
  // src/v1/cli.ts
2251
2517
  init_cjs_shims();
2252
2518
  async function main() {
@@ -2306,6 +2572,12 @@ async function main() {
2306
2572
  serve({ fetch: app.fetch, hostname: host, port }, (info) => {
2307
2573
  console.log(`Listening on http://${host}:${port}`);
2308
2574
  });
2575
+ try {
2576
+ const { startStorageMaintenance: startStorageMaintenance2 } = await Promise.resolve().then(() => (init_maintenance(), maintenance_exports));
2577
+ startStorageMaintenance2("default");
2578
+ } catch (err) {
2579
+ console.error(`[AgentGate] \u5B58\u50A8\u7EF4\u62A4\u542F\u52A8\u5931\u8D25: ${String(err?.message ?? err)}`);
2580
+ }
2309
2581
  }
2310
2582
  main().catch((err) => {
2311
2583
  console.error("Fatal error:", err);