@kenhkl/agent-gate 0.5.4 → 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.4" : (() => {
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) {
@@ -946,11 +969,10 @@ async function analyzeRoute(c) {
946
969
  const hostname = context.os?.hostname || "unknown";
947
970
  const agentId = getOrCreateAgent(input.agent_type, hostname, input.agent_version);
948
971
  getOrCreateSession(input.session_id, agentId, input.cwd, input.user_prompt, String(context.os?.system || ""));
949
- const seq2 = getNextSeq(input.session_id);
950
972
  const mossResult = await callAgentMoss(mossRequest);
951
973
  insertToolCall({
952
974
  session_id: input.session_id,
953
- seq: seq2,
975
+ seq: getNextSeq(input.session_id),
954
976
  tool_name: toolName,
955
977
  command,
956
978
  description: input.description,