@kenhkl/agent-gate 0.8.2 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/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 recent = queryToolCalls2({ limit: scanLimit });
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, import_path.join)(home, ".local", "share");
636
- return (0, import_path.join)(xdgData, "agent_gate", "storage");
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 import_fs, import_path, JsonStorage;
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
- import_fs = require("fs");
644
- import_path = require("path");
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, import_path.join)(this.dataDir, "agents.json");
659
- this.sessionsPath = (0, import_path.join)(this.dataDir, "sessions.json");
660
- this.toolCallsDir = (0, import_path.join)(this.dataDir, "tool_calls");
661
- if (!(0, import_fs.existsSync)(this.dataDir)) (0, import_fs.mkdirSync)(this.dataDir, { recursive: true, mode: 448 });
662
- if (!(0, import_fs.existsSync)(this.toolCallsDir)) (0, import_fs.mkdirSync)(this.toolCallsDir, { recursive: true, mode: 448 });
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, import_fs.statSync)(p);
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, import_fs.chmodSync)(p, mode);
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, import_fs.readdirSync)(this.toolCallsDir)) {
768
+ for (const f of (0, import_fs2.readdirSync)(this.toolCallsDir)) {
687
769
  if (f.endsWith(".json") || f.endsWith(".tmp")) {
688
- tighten((0, import_path.join)(this.toolCallsDir, f), 384);
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, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
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, import_path.join)(this.toolCallsDir, file), []);
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, import_fs.existsSync)(path)) return fallback;
712
- return JSON.parse((0, import_fs.readFileSync)(path, "utf-8"));
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, import_path.dirname)(path);
720
- if (!(0, import_fs.existsSync)(dir)) (0, import_fs.mkdirSync)(dir, { recursive: true, mode: 448 });
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, import_fs.writeFileSync)(tmp, JSON.stringify(data, null, 2), { encoding: "utf-8", mode: 384 });
723
- (0, import_fs.renameSync)(tmp, path);
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, import_path.join)(this.toolCallsDir, `${sessionId}.json`),
823
+ (0, import_path2.join)(this.toolCallsDir, `${sessionId}.json`),
742
824
  []
743
825
  );
744
826
  }
745
827
  saveToolCalls(sessionId, records) {
746
- this.writeJsonFile((0, import_path.join)(this.toolCallsDir, `${sessionId}.json`), records);
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, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
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, import_path.join)(this.toolCallsDir, file), []);
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 {
@@ -836,9 +922,21 @@ var init_json_storage = __esm({
836
922
  if (params.agentType) {
837
923
  allRecords = allRecords.filter((r) => r.agent_type === params.agentType);
838
924
  }
925
+ if (params.agentId) {
926
+ allRecords = allRecords.filter((r) => `${r.agent_type}:${r.hostname}` === params.agentId);
927
+ }
839
928
  if (params.decision) {
840
929
  allRecords = allRecords.filter((r) => r.decision === params.decision);
841
930
  }
931
+ if (params.riskLevel) {
932
+ allRecords = allRecords.filter((r) => r.risk_level === params.riskLevel);
933
+ }
934
+ if (params.fromTime) {
935
+ allRecords = allRecords.filter((r) => r.called_at >= params.fromTime);
936
+ }
937
+ if (params.toTime) {
938
+ allRecords = allRecords.filter((r) => r.called_at <= params.toTime);
939
+ }
842
940
  allRecords.sort((a, b) => b.called_at.localeCompare(a.called_at));
843
941
  const offset = params.offset || 0;
844
942
  const limit = params.limit || 50;
@@ -849,13 +947,22 @@ var init_json_storage = __esm({
849
947
  const agents = this.loadAgents();
850
948
  let allRecords = [];
851
949
  try {
852
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
950
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
853
951
  for (const file of files) {
854
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
952
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
855
953
  allRecords.push(...records);
856
954
  }
857
955
  } catch {
858
956
  }
957
+ if (params.sessionId) {
958
+ allRecords = allRecords.filter((r) => r.session_id === params.sessionId);
959
+ }
960
+ if (params.agentId) {
961
+ const allowed = new Set(
962
+ [...sessions.values()].filter((s) => s.agent_id === params.agentId).map((s) => s.id)
963
+ );
964
+ allRecords = allRecords.filter((r) => allowed.has(r.session_id));
965
+ }
859
966
  if (params.agentType) {
860
967
  const allowed = new Set(
861
968
  [...sessions.values()].filter((s) => (agents.get(s.agent_id)?.agent_type || "") === params.agentType).map((s) => s.id)
@@ -865,6 +972,15 @@ var init_json_storage = __esm({
865
972
  if (params.decision) {
866
973
  allRecords = allRecords.filter((r) => r.decision === params.decision);
867
974
  }
975
+ if (params.riskLevel) {
976
+ allRecords = allRecords.filter((r) => r.risk_level === params.riskLevel);
977
+ }
978
+ if (params.fromTime) {
979
+ allRecords = allRecords.filter((r) => r.called_at >= params.fromTime);
980
+ }
981
+ if (params.toTime) {
982
+ allRecords = allRecords.filter((r) => r.called_at <= params.toTime);
983
+ }
868
984
  return allRecords.length;
869
985
  }
870
986
  getStorageStats() {
@@ -872,12 +988,12 @@ var init_json_storage = __esm({
872
988
  let jsonFiles = 0;
873
989
  let jsonBytes = 0;
874
990
  try {
875
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
991
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
876
992
  for (const file of files) {
877
- const stat = (0, import_fs.statSync)((0, import_path.join)(this.toolCallsDir, file));
993
+ const stat = (0, import_fs2.statSync)((0, import_path2.join)(this.toolCallsDir, file));
878
994
  jsonFiles++;
879
995
  jsonBytes += stat.size;
880
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
996
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
881
997
  toolCalls += records.length;
882
998
  }
883
999
  } catch {
@@ -887,9 +1003,9 @@ var init_json_storage = __esm({
887
1003
  clearToolCalls() {
888
1004
  let removed = 0;
889
1005
  try {
890
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
1006
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
891
1007
  for (const file of files) {
892
- (0, import_fs.unlinkSync)((0, import_path.join)(this.toolCallsDir, file));
1008
+ (0, import_fs2.unlinkSync)((0, import_path2.join)(this.toolCallsDir, file));
893
1009
  removed++;
894
1010
  }
895
1011
  } catch {
@@ -907,6 +1023,49 @@ var init_json_storage = __esm({
907
1023
  hostname: agent?.hostname || ""
908
1024
  };
909
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
+ }
910
1069
  getAgentsList(agentType) {
911
1070
  const agents = this.loadAgents();
912
1071
  const sessions = this.loadSessions();
@@ -941,9 +1100,9 @@ var init_json_storage = __esm({
941
1100
  getTimeline(limit = 100) {
942
1101
  const buckets = /* @__PURE__ */ new Map();
943
1102
  try {
944
- const files = (0, import_fs.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
1103
+ const files = (0, import_fs2.readdirSync)(this.toolCallsDir).filter((f) => f.endsWith(".json"));
945
1104
  for (const file of files) {
946
- const records = this.readJsonFile((0, import_path.join)(this.toolCallsDir, file), []);
1105
+ const records = this.readJsonFile((0, import_path2.join)(this.toolCallsDir, file), []);
947
1106
  for (const r of records) {
948
1107
  const minute = r.called_at.replace(/(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}/, "$1T$2");
949
1108
  const bucket = buckets.get(minute) || { allow: 0, deny: 0 };
@@ -974,16 +1133,17 @@ __export(sqlite_storage_exports, {
974
1133
  });
975
1134
  function getDefaultDbPath() {
976
1135
  const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
977
- const xdgData = process.env.XDG_DATA_HOME || (0, import_path2.join)(home, ".local", "share");
978
- return (0, import_path2.join)(xdgData, "agent_gate", "agent_gate_ts.db");
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");
979
1138
  }
980
- var import_fs2, import_path2, SCHEMA, SqliteStorage;
1139
+ var import_fs3, import_path3, SCHEMA, SqliteStorage;
981
1140
  var init_sqlite_storage = __esm({
982
1141
  "src/shared/storage/sqlite-storage.ts"() {
983
1142
  "use strict";
984
1143
  init_cjs_shims();
985
- import_fs2 = require("fs");
986
- import_path2 = require("path");
1144
+ import_fs3 = require("fs");
1145
+ import_path3 = require("path");
1146
+ init_record_guard();
987
1147
  SCHEMA = `
988
1148
  CREATE TABLE IF NOT EXISTS agents (
989
1149
  id TEXT PRIMARY KEY,
@@ -1043,8 +1203,8 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1043
1203
  );
1044
1204
  }
1045
1205
  const path = dbPath || getDefaultDbPath();
1046
- const dir = (0, import_path2.dirname)(path);
1047
- if (!(0, import_fs2.existsSync)(dir)) (0, import_fs2.mkdirSync)(dir, { recursive: true });
1206
+ const dir = (0, import_path3.dirname)(path);
1207
+ if (!(0, import_fs3.existsSync)(dir)) (0, import_fs3.mkdirSync)(dir, { recursive: true });
1048
1208
  this.dbPath = path;
1049
1209
  this.db = new Database(path);
1050
1210
  this.db.pragma("journal_mode = WAL");
@@ -1078,6 +1238,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1078
1238
  return row.next;
1079
1239
  }
1080
1240
  insertToolCall(record) {
1241
+ record = prepareRecordForStorage(
1242
+ record,
1243
+ this.dbPath
1244
+ ).record;
1081
1245
  const result = this.db.transaction((rec) => {
1082
1246
  const row = this.db.prepare(
1083
1247
  "SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
@@ -1121,6 +1285,10 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1121
1285
  conditions.push("tc.session_id = ?");
1122
1286
  values.push(params.sessionId);
1123
1287
  }
1288
+ if (params.agentId) {
1289
+ conditions.push("s.agent_id = ?");
1290
+ values.push(params.agentId);
1291
+ }
1124
1292
  if (params.agentType) {
1125
1293
  conditions.push("a.agent_type = ?");
1126
1294
  values.push(params.agentType);
@@ -1129,6 +1297,18 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1129
1297
  conditions.push("tc.decision = ?");
1130
1298
  values.push(params.decision);
1131
1299
  }
1300
+ if (params.riskLevel) {
1301
+ conditions.push("tc.risk_level = ?");
1302
+ values.push(params.riskLevel);
1303
+ }
1304
+ if (params.fromTime) {
1305
+ conditions.push("tc.called_at >= ?");
1306
+ values.push(params.fromTime);
1307
+ }
1308
+ if (params.toTime) {
1309
+ conditions.push("tc.called_at <= ?");
1310
+ values.push(params.toTime);
1311
+ }
1132
1312
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1133
1313
  const limit = params.limit || 50;
1134
1314
  const offset = params.offset || 0;
@@ -1143,14 +1323,34 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1143
1323
  countToolCalls(params) {
1144
1324
  const conditions = [];
1145
1325
  const values = [];
1146
- if (params.decision) {
1147
- conditions.push("tc.decision = ?");
1148
- values.push(params.decision);
1326
+ if (params.sessionId) {
1327
+ conditions.push("tc.session_id = ?");
1328
+ values.push(params.sessionId);
1329
+ }
1330
+ if (params.agentId) {
1331
+ conditions.push("s.agent_id = ?");
1332
+ values.push(params.agentId);
1149
1333
  }
1150
1334
  if (params.agentType) {
1151
1335
  conditions.push("a.agent_type = ?");
1152
1336
  values.push(params.agentType);
1153
1337
  }
1338
+ if (params.decision) {
1339
+ conditions.push("tc.decision = ?");
1340
+ values.push(params.decision);
1341
+ }
1342
+ if (params.riskLevel) {
1343
+ conditions.push("tc.risk_level = ?");
1344
+ values.push(params.riskLevel);
1345
+ }
1346
+ if (params.fromTime) {
1347
+ conditions.push("tc.called_at >= ?");
1348
+ values.push(params.fromTime);
1349
+ }
1350
+ if (params.toTime) {
1351
+ conditions.push("tc.called_at <= ?");
1352
+ values.push(params.toTime);
1353
+ }
1154
1354
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1155
1355
  const row = this.db.prepare(
1156
1356
  `SELECT COUNT(*) as total FROM tool_calls tc
@@ -1182,6 +1382,49 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
1182
1382
  })();
1183
1383
  return result;
1184
1384
  }
1385
+ /**
1386
+ * 把最老的记录归档到本地压缩文件,主库只保留最近 `keep` 条。
1387
+ *
1388
+ * 归档文件是 **JSONL + gzip**(一行一条记录),放在 `<数据库目录>/archive/` 下,
1389
+ * 需要翻旧账时用 `zcat archive/xxx.jsonl.gz | grep ...` 即可 —— 数据不丢,只是
1390
+ * 不在主库里了。写完文件、校验落盘后才删主库行,最后 VACUUM 真正释放磁盘。
1391
+ */
1392
+ archiveOldRecords(keep) {
1393
+ if (keep <= 0) return { archived: 0, file: "" };
1394
+ const total = this.db.prepare("SELECT COUNT(*) as n FROM tool_calls").get().n;
1395
+ if (total <= keep) return { archived: 0, file: "" };
1396
+ const cutoff = this.db.prepare(
1397
+ "SELECT id FROM tool_calls ORDER BY id LIMIT 1 OFFSET ?"
1398
+ ).get(total - keep - 1);
1399
+ if (!cutoff) return { archived: 0, file: "" };
1400
+ const rows = this.db.prepare(
1401
+ "SELECT * FROM tool_calls WHERE id <= ? ORDER BY id"
1402
+ ).all(cutoff.id);
1403
+ if (rows.length === 0) return { archived: 0, file: "" };
1404
+ const { writeFileSync: writeFileSync3, mkdirSync: mkdirSync4, existsSync: existsSync3 } = require("fs");
1405
+ const { dirname: dirname4, join: join4 } = require("path");
1406
+ const { gzipSync } = require("zlib");
1407
+ const archiveDir = join4(dirname4(this.dbPath), "archive");
1408
+ if (!existsSync3(archiveDir)) mkdirSync4(archiveDir, { recursive: true, mode: 448 });
1409
+ const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
1410
+ const file = join4(archiveDir, `tool_calls-${stamp}.jsonl.gz`);
1411
+ const payload = rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
1412
+ writeFileSync3(file, gzipSync(Buffer.from(payload, "utf-8")), { mode: 384 });
1413
+ const deleted = this.db.transaction(() => {
1414
+ const res = this.db.prepare("DELETE FROM tool_calls WHERE id <= ?").run(cutoff.id);
1415
+ this.db.prepare(`
1416
+ UPDATE sessions SET tool_call_count = (
1417
+ SELECT COUNT(*) FROM tool_calls WHERE tool_calls.session_id = sessions.id
1418
+ )
1419
+ `).run();
1420
+ return Number(res.changes);
1421
+ })();
1422
+ return { archived: deleted, file };
1423
+ }
1424
+ /** 真正释放磁盘空间:DELETE 后必须 VACUUM,否则文件不会变小。 */
1425
+ vacuum() {
1426
+ this.db.exec("VACUUM");
1427
+ }
1185
1428
  getAgentsList(agentType) {
1186
1429
  const where = agentType ? "WHERE a.agent_type = ?" : "";
1187
1430
  return this.db.prepare(`
@@ -1273,12 +1516,12 @@ function getDb(dbPath) {
1273
1516
  try {
1274
1517
  const Database = require("better-sqlite3");
1275
1518
  const { existsSync: existsSync3, mkdirSync: mkdirSync4 } = require("fs");
1276
- const { join: join4, dirname: dirname3 } = require("path");
1519
+ const { join: join4, dirname: dirname4 } = require("path");
1277
1520
  const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
1278
1521
  const xdgData = process.env.XDG_DATA_HOME || join4(home, ".local", "share");
1279
1522
  const defaultPath = join4(xdgData, "agent_gate", "agent_gate_ts.db");
1280
1523
  const path = dbPath || defaultPath;
1281
- const dir = dirname3(path);
1524
+ const dir = dirname4(path);
1282
1525
  if (!existsSync3(dir)) mkdirSync4(dir, { recursive: true });
1283
1526
  rawDb = new Database(path);
1284
1527
  rawDb.pragma("journal_mode = WAL");
@@ -1343,7 +1586,7 @@ var import_zod = require("zod");
1343
1586
 
1344
1587
  // src/version.ts
1345
1588
  init_cjs_shims();
1346
- var _version = true ? "0.8.2" : (() => {
1589
+ var _version = true ? "0.9.1" : (() => {
1347
1590
  try {
1348
1591
  const require2 = createRequire(importMetaUrl);
1349
1592
  const pkg = require2("../package.json");
@@ -1372,18 +1615,23 @@ var AgentGateInputSchema = import_zod.z.object({
1372
1615
  text: import_zod.z.string().default(""),
1373
1616
  actions: import_zod.z.array(import_zod.z.object({
1374
1617
  action_type: import_zod.z.string(),
1375
- action_detail: import_zod.z.string()
1618
+ action_detail: import_zod.z.string(),
1619
+ output: import_zod.z.string().optional().describe("\u5DE5\u5177\u8FD4\u56DE\u5185\u5BB9\uFF1Bask_user_question \u7684\u7528\u6237\u56DE\u7B54\u9760\u5B83\u4F20\u7ED9 AgentMoss \u5224\u6388\u6743")
1376
1620
  })).default([])
1377
1621
  })).default([]),
1378
1622
  action_history: import_zod.z.array(import_zod.z.object({
1379
1623
  action_type: import_zod.z.string(),
1380
- action_detail: import_zod.z.string()
1624
+ action_detail: import_zod.z.string(),
1625
+ // 授权类规则(改密码 / 删用户)靠 ask_user_question 的 output 判断用户是否已确认。
1626
+ // 此前 schema 里没有这个字段,Agent 传了也会被剥掉 → 必然判成「未授权」。
1627
+ output: import_zod.z.string().optional().describe("\u5DE5\u5177\u8FD4\u56DE\u5185\u5BB9\uFF1Bask_user_question \u7684\u7528\u6237\u56DE\u7B54\u9760\u5B83\u4F20\u7ED9 AgentMoss \u5224\u6388\u6743")
1381
1628
  })).default([]),
1382
1629
  session_history: import_zod.z.array(import_zod.z.object({
1383
1630
  session_id: import_zod.z.string(),
1384
1631
  action_history: import_zod.z.array(import_zod.z.object({
1385
1632
  action_type: import_zod.z.string(),
1386
- action_detail: import_zod.z.string()
1633
+ action_detail: import_zod.z.string(),
1634
+ output: import_zod.z.string().optional()
1387
1635
  })).default([])
1388
1636
  })).default([]).describe("\u591A session \u5386\u53F2\u52A8\u4F5C\uFF08\u53EF\u9009\uFF09\uFF1A\u4F9B AgentMoss L2.4 \u5F62\u5F0F\u5316\u5F15\u64CE\u8DE8\u4F1A\u8BDD\u9A8C\u8BC1"),
1389
1637
  metadata: import_zod.z.record(import_zod.z.unknown()).optional()
@@ -1428,20 +1676,23 @@ init_multi_session();
1428
1676
  function normalize(input, systemContext) {
1429
1677
  const actionHistory = input.action_history.map((h) => ({
1430
1678
  action_type: h.action_type,
1431
- action_detail: h.action_detail
1679
+ action_detail: h.action_detail,
1680
+ output: h.output ?? ""
1432
1681
  }));
1433
1682
  const sessionHistory = (input.session_history || []).map((s) => ({
1434
1683
  session_id: s.session_id,
1435
1684
  action_history: s.action_history.map((h) => ({
1436
1685
  action_type: h.action_type,
1437
- action_detail: h.action_detail
1686
+ action_detail: h.action_detail,
1687
+ output: h.output ?? ""
1438
1688
  }))
1439
1689
  }));
1440
1690
  const promptHistory = input.prompt_history.map((t) => ({
1441
1691
  text: t.text,
1442
1692
  actions: t.actions.map((a) => ({
1443
1693
  action_type: a.action_type,
1444
- action_detail: a.action_detail
1694
+ action_detail: a.action_detail,
1695
+ output: a.output ?? ""
1445
1696
  }))
1446
1697
  }));
1447
1698
  const toolName = input.tool_name || input.a_next?.action_type || "unknown";
@@ -1671,7 +1922,9 @@ async function analyzeRoute(c) {
1671
1922
  if (isMultiSessionEnabled()) {
1672
1923
  input.session_history = buildSessionHistory(
1673
1924
  (params) => queryToolCalls(params),
1674
- input.session_id
1925
+ input.session_id,
1926
+ // 多 agent 时按 agent 分组取历史,避免高频 agent 把别人的动作挤出窗口
1927
+ () => getAgentsList().map((a) => a.agent_type)
1675
1928
  );
1676
1929
  }
1677
1930
  const mossRequest = normalize(input, context);
@@ -1755,13 +2008,22 @@ async function sessionsRoute(c) {
1755
2008
  return c.json({ session_id: sessionId, tool_calls: rows });
1756
2009
  }
1757
2010
  async function toolCallsRoute(c) {
1758
- const decision = c.req.query("decision");
1759
- const limit = parseInt(c.req.query("limit") || "50", 10);
1760
- const offset = parseInt(c.req.query("offset") || "0", 10);
2011
+ const sessionId = c.req.query("session_id") || void 0;
2012
+ let agentId = c.req.query("agent_id") || void 0;
2013
+ const decision = c.req.query("decision") || void 0;
2014
+ const riskLevel = c.req.query("risk_level") || void 0;
2015
+ const fromTime = c.req.query("from") || void 0;
2016
+ const toTime = c.req.query("to") || void 0;
2017
+ const rawLimit = parseInt(c.req.query("limit") || "50", 10);
2018
+ const limit = Number.isFinite(rawLimit) ? Math.min(Math.max(rawLimit, 1), 500) : 50;
2019
+ const rawOffset = parseInt(c.req.query("offset") || "0", 10);
2020
+ const offset = Number.isFinite(rawOffset) && rawOffset > 0 ? rawOffset : 0;
1761
2021
  const bound = boundAgentOf(c.get("principal") || "");
2022
+ if (bound) agentId = void 0;
1762
2023
  const agentType = bound || void 0;
1763
- const total = countToolCalls({ decision: decision ?? void 0, agentType });
1764
- const data = queryToolCalls({ decision: decision ?? void 0, agentType, limit, offset });
2024
+ const filter = { sessionId, agentId, agentType, decision, riskLevel, fromTime, toTime };
2025
+ const total = countToolCalls(filter);
2026
+ const data = queryToolCalls({ ...filter, limit, offset });
1765
2027
  return c.json({ total, data });
1766
2028
  }
1767
2029
  async function agentsRoute(c) {
@@ -1960,9 +2222,9 @@ async function dashboardRoute(c) {
1960
2222
  }
1961
2223
  }
1962
2224
  const { readFileSync: readFileSync3, existsSync: existsSync3 } = await import("fs");
1963
- const { resolve, dirname: dirname3 } = await import("path");
2225
+ const { resolve, dirname: dirname4 } = await import("path");
1964
2226
  const { fileURLToPath } = await import("url");
1965
- const __dirname = dirname3(fileURLToPath(importMetaUrl));
2227
+ const __dirname = dirname4(fileURLToPath(importMetaUrl));
1966
2228
  const paths = [
1967
2229
  resolve(__dirname, "dashboard.html"),
1968
2230
  resolve(__dirname, "../dashboard.html"),