@kenhkl/agent-gate 0.5.3 → 0.5.5

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/cli.js CHANGED
@@ -50,7 +50,7 @@ var init_version = __esm({
50
50
  "src/version.ts"() {
51
51
  "use strict";
52
52
  init_cjs_shims();
53
- _version = true ? "0.5.3" : (() => {
53
+ _version = true ? "0.5.5" : (() => {
54
54
  try {
55
55
  const require2 = createRequire(importMetaUrl);
56
56
  const pkg = require2("../package.json");
@@ -89,6 +89,8 @@ var init_json_storage = __esm({
89
89
  sessionsPath;
90
90
  toolCallsDir;
91
91
  nextId;
92
+ /** per-session 互斥锁,保证同一 session 的 seq 生成 + 写入原子性 */
93
+ sessionLocks = /* @__PURE__ */ new Map();
92
94
  constructor(dataDir) {
93
95
  this.dataDir = dataDir || process.env.AGENT_GATE_DATA_DIR || getDefaultDataDir();
94
96
  this.agentsPath = (0, import_path.join)(this.dataDir, "agents.json");
@@ -195,11 +197,26 @@ var init_json_storage = __esm({
195
197
  return Math.max(...records.map((r) => r.seq)) + 1;
196
198
  }
197
199
  insertToolCall(record) {
198
- const records = this.loadToolCalls(record.session_id);
199
- const id = this.nextId++;
200
- records.push({ ...record, id, called_at: nowISO() });
201
- this.saveToolCalls(record.session_id, records);
202
- return id;
200
+ const sessionId = record.session_id;
201
+ const prev = this.sessionLocks.get(sessionId) ?? Promise.resolve();
202
+ const next = prev.then(() => {
203
+ const records = this.loadToolCalls(sessionId);
204
+ const existingSeq = records.length === 0 ? 0 : Math.max(...records.map((r) => r.seq));
205
+ record.seq = existingSeq + 1;
206
+ const id = this.nextId++;
207
+ records.push({ ...record, id, called_at: nowISO() });
208
+ this.saveToolCalls(sessionId, records);
209
+ return id;
210
+ });
211
+ this.sessionLocks.set(sessionId, next.catch(() => {
212
+ }));
213
+ const cleanup = () => {
214
+ if (this.sessionLocks.get(sessionId) === next) {
215
+ this.sessionLocks.delete(sessionId);
216
+ }
217
+ };
218
+ next.then(cleanup, cleanup);
219
+ return 0;
203
220
  }
204
221
  getActionHistory(sessionId, limit = 100) {
205
222
  const records = this.loadToolCalls(sessionId);
@@ -422,28 +439,34 @@ CREATE INDEX IF NOT EXISTS idx_tool_calls_decision ON tool_calls(decision, calle
422
439
  return row.next;
423
440
  }
424
441
  insertToolCall(record) {
425
- const result = this.db.prepare(`
426
- INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
427
- action_type, action_detail, decision, risk_level, risk_type, violated_layers,
428
- reason, confidence, analysis_duration_ms)
429
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
430
- `).run(
431
- record.session_id,
432
- record.seq,
433
- record.tool_name,
434
- record.command,
435
- record.description,
436
- record.cwd,
437
- record.action_type,
438
- record.action_detail,
439
- record.decision,
440
- record.risk_level,
441
- record.risk_type,
442
- record.violated_layers,
443
- record.reason,
444
- record.confidence,
445
- record.analysis_duration_ms
446
- );
442
+ const result = this.db.transaction((rec) => {
443
+ const row = this.db.prepare(
444
+ "SELECT COALESCE(MAX(seq), 0) + 1 as next FROM tool_calls WHERE session_id = ?"
445
+ ).get(rec.session_id);
446
+ const seq2 = row.next;
447
+ return this.db.prepare(`
448
+ INSERT INTO tool_calls (session_id, seq, tool_name, command, description, cwd,
449
+ action_type, action_detail, decision, risk_level, risk_type, violated_layers,
450
+ reason, confidence, analysis_duration_ms)
451
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
452
+ `).run(
453
+ rec.session_id,
454
+ seq2,
455
+ rec.tool_name,
456
+ rec.command,
457
+ rec.description,
458
+ rec.cwd,
459
+ rec.action_type,
460
+ rec.action_detail,
461
+ rec.decision,
462
+ rec.risk_level,
463
+ rec.risk_type,
464
+ rec.violated_layers,
465
+ rec.reason,
466
+ rec.confidence,
467
+ rec.analysis_duration_ms
468
+ );
469
+ })(record);
447
470
  return Number(result.lastInsertRowid);
448
471
  }
449
472
  getActionHistory(sessionId, limit = 100) {
@@ -613,11 +636,11 @@ function setMossUrl(url) {
613
636
  }
614
637
  async function callAgentMoss(request) {
615
638
  try {
616
- const resp = await fetch(`${mossUrl}/api/v1/analyze`, {
639
+ const resp = await (0, import_node_fetch.default)(`${mossUrl}/api/v1/analyze`, {
617
640
  method: "POST",
618
641
  headers: { "Content-Type": "application/json" },
619
642
  body: JSON.stringify(request),
620
- signal: AbortSignal.timeout(3e4)
643
+ timeout: 3e4
621
644
  });
622
645
  if (!resp.ok) {
623
646
  console.error(`[AgentGate] AgentMoss HTTP ${resp.status}: ${resp.statusText}`);
@@ -631,8 +654,8 @@ async function callAgentMoss(request) {
631
654
  }
632
655
  async function fetchMossHealth() {
633
656
  try {
634
- const resp = await fetch(`${mossUrl}/api/v1/health`, {
635
- signal: AbortSignal.timeout(3e3)
657
+ const resp = await (0, import_node_fetch.default)(`${mossUrl}/api/v1/health`, {
658
+ timeout: 3e3
636
659
  });
637
660
  if (!resp.ok) {
638
661
  return { moss_version: "N/A", moss_status: "error" };
@@ -656,11 +679,12 @@ function failClosedResponse(reason) {
656
679
  confidence: 100
657
680
  };
658
681
  }
659
- var DEFAULT_MOSS_URL, mossUrl;
682
+ var import_node_fetch, DEFAULT_MOSS_URL, mossUrl;
660
683
  var init_moss_client = __esm({
661
684
  "src/shared/moss-client.ts"() {
662
685
  "use strict";
663
686
  init_cjs_shims();
687
+ import_node_fetch = __toESM(require("node-fetch"));
664
688
  DEFAULT_MOSS_URL = "http://127.0.0.1:9090";
665
689
  mossUrl = DEFAULT_MOSS_URL;
666
690
  }
@@ -945,11 +969,10 @@ async function analyzeRoute(c) {
945
969
  const hostname = context.os?.hostname || "unknown";
946
970
  const agentId = getOrCreateAgent(input.agent_type, hostname, input.agent_version);
947
971
  getOrCreateSession(input.session_id, agentId, input.cwd, input.user_prompt, String(context.os?.system || ""));
948
- const seq2 = getNextSeq(input.session_id);
949
972
  const mossResult = await callAgentMoss(mossRequest);
950
973
  insertToolCall({
951
974
  session_id: input.session_id,
952
- seq: seq2,
975
+ seq: getNextSeq(input.session_id),
953
976
  tool_name: toolName,
954
977
  command,
955
978
  description: input.description,
@@ -29434,6 +29457,7 @@ init_cjs_shims();
29434
29457
  var import_node_fs4 = require("fs");
29435
29458
  var import_yaml3 = require("yaml");
29436
29459
  init_version();
29460
+ var import_node_fetch2 = __toESM(require("node-fetch"));
29437
29461
  async function main() {
29438
29462
  const args = process.argv.slice(2);
29439
29463
  const command = args[0];
@@ -29835,7 +29859,7 @@ Run options:
29835
29859
  const mossUrl2 = process.env.AGENT_MOSS_URL || "http://127.0.0.1:9090";
29836
29860
  let mossConnected = false;
29837
29861
  try {
29838
- const resp = await fetch(`${mossUrl2}/api/v1/health`, { signal: AbortSignal.timeout(3e3) });
29862
+ const resp = await (0, import_node_fetch2.default)(`${mossUrl2}/api/v1/health`, { timeout: 3e3 });
29839
29863
  if (resp.status === 200) {
29840
29864
  mossConnected = true;
29841
29865
  console.log(`[AgentGate] AgentMoss connected: ${mossUrl2}`);