negotium 0.1.16 → 0.1.17

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/cron.js CHANGED
@@ -3214,7 +3214,7 @@ var init_claude_provider = __esm(async () => {
3214
3214
  });
3215
3215
 
3216
3216
  // ../../packages/core/src/version.ts
3217
- var NEGOTIUM_VERSION = "0.1.16";
3217
+ var NEGOTIUM_VERSION = "0.1.17";
3218
3218
 
3219
3219
  // ../../packages/core/src/agents/codex-native-multi-agent.ts
3220
3220
  import { spawn as spawn3 } from "child_process";
@@ -6436,6 +6436,25 @@ var init_derive = __esm(async () => {
6436
6436
 
6437
6437
  // ../../packages/core/src/storage/runtime-turn-requests.ts
6438
6438
  import { randomUUID as randomUUID5 } from "crypto";
6439
+ function createRuntimeUserTurnRequestsTable() {
6440
+ db.exec(`
6441
+ CREATE TABLE IF NOT EXISTS runtime_user_turn_requests (
6442
+ request_id TEXT PRIMARY KEY,
6443
+ topic_id TEXT NOT NULL,
6444
+ user_id TEXT NOT NULL,
6445
+ prompt TEXT NOT NULL,
6446
+ attachments_json TEXT,
6447
+ allow_auto_continue INTEGER NOT NULL DEFAULT 1 CHECK (allow_auto_continue IN (0, 1)),
6448
+ execution_json TEXT,
6449
+ topic_epoch INTEGER NOT NULL DEFAULT 0,
6450
+ created_at INTEGER NOT NULL,
6451
+ status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'running')),
6452
+ claimed_by TEXT,
6453
+ claimed_at INTEGER,
6454
+ running_query_id TEXT
6455
+ )
6456
+ `);
6457
+ }
6439
6458
  function rowToRequest(row) {
6440
6459
  let attachments;
6441
6460
  if (row.attachments_json) {
@@ -6479,24 +6498,17 @@ function enqueueRuntimeUserTurnRequest(input) {
6479
6498
  const requestId = input.requestId ?? randomUUID5();
6480
6499
  const now = Date.now();
6481
6500
  const topicEpoch = input.topicEpoch ?? getRuntimeTopicEpoch(input.topicId);
6482
- db.query(`INSERT INTO runtime_user_turn_requests
6483
- (topic_id, request_id, user_id, prompt, attachments_json,
6501
+ db.transaction(() => {
6502
+ if (input.supersedeExisting !== false) {
6503
+ db.query("DELETE FROM runtime_user_turn_requests WHERE topic_id = ?").run(input.topicId);
6504
+ }
6505
+ db.query(`INSERT INTO runtime_user_turn_requests
6506
+ (request_id, topic_id, user_id, prompt, attachments_json,
6484
6507
  allow_auto_continue, execution_json, topic_epoch, created_at,
6485
6508
  status, claimed_by, claimed_at, running_query_id)
6486
6509
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', NULL, NULL, NULL)
6487
- ON CONFLICT(topic_id) DO UPDATE SET
6488
- request_id = excluded.request_id,
6489
- user_id = excluded.user_id,
6490
- prompt = excluded.prompt,
6491
- attachments_json = excluded.attachments_json,
6492
- allow_auto_continue = excluded.allow_auto_continue,
6493
- execution_json = excluded.execution_json,
6494
- topic_epoch = excluded.topic_epoch,
6495
- created_at = excluded.created_at,
6496
- status = 'pending',
6497
- claimed_by = NULL,
6498
- claimed_at = NULL,
6499
- running_query_id = NULL`).run(input.topicId, requestId, input.userId, input.prompt, input.attachments?.length ? JSON.stringify(input.attachments) : null, input.allowAutoContinue ? 1 : 0, input.execution ? JSON.stringify(input.execution) : null, topicEpoch, now);
6510
+ ON CONFLICT(request_id) DO NOTHING`).run(requestId, input.topicId, input.userId, input.prompt, input.attachments?.length ? JSON.stringify(input.attachments) : null, input.allowAutoContinue ? 1 : 0, input.execution ? JSON.stringify(input.execution) : null, topicEpoch, now);
6511
+ })();
6500
6512
  return requestId;
6501
6513
  }
6502
6514
  function claimNextRuntimeUserTurnRequest(ownerId, now = Date.now()) {
@@ -6508,21 +6520,29 @@ function claimNextRuntimeUserTurnRequest(ownerId, now = Date.now()) {
6508
6520
  WHERE (l.topic_id IS NULL OR l.heartbeat_at < ?)
6509
6521
  AND (s.topic_id IS NULL OR s.maintenance = 0 OR s.heartbeat_at < ?)
6510
6522
  AND r.topic_epoch = COALESCE(s.epoch, 0)
6523
+ AND NOT EXISTS (
6524
+ SELECT 1
6525
+ FROM runtime_user_turn_requests active
6526
+ WHERE active.topic_id = r.topic_id
6527
+ AND active.request_id <> r.request_id
6528
+ AND active.claimed_at IS NOT NULL
6529
+ AND active.claimed_at >= ?
6530
+ )
6511
6531
  AND (
6512
6532
  (r.status = 'pending' AND (r.claimed_at IS NULL OR r.claimed_at < ?))
6513
6533
  OR (r.status = 'running' AND (r.claimed_at IS NULL OR r.claimed_at < ?))
6514
6534
  )
6515
- ORDER BY r.created_at ASC
6516
- LIMIT 1`).get(now - TURN_LEASE_STALE_MS, now - TOPIC_MAINTENANCE_STALE_MS, now - REQUEST_CLAIM_STALE_MS, now - REQUEST_CLAIM_STALE_MS);
6535
+ ORDER BY r.created_at ASC, r.rowid ASC
6536
+ LIMIT 1`).get(now - TURN_LEASE_STALE_MS, now - TOPIC_MAINTENANCE_STALE_MS, now - REQUEST_CLAIM_STALE_MS, now - REQUEST_CLAIM_STALE_MS, now - REQUEST_CLAIM_STALE_MS);
6517
6537
  if (!row)
6518
6538
  return null;
6519
6539
  const updated = db.query(`UPDATE runtime_user_turn_requests
6520
6540
  SET claimed_by = ?, claimed_at = ?
6521
- WHERE topic_id = ? AND request_id = ?
6541
+ WHERE request_id = ? AND topic_id = ?
6522
6542
  AND (
6523
6543
  (status = 'pending' AND (claimed_at IS NULL OR claimed_at < ?))
6524
6544
  OR (status = 'running' AND (claimed_at IS NULL OR claimed_at < ?))
6525
- )`).run(ownerId, now, row.topic_id, row.request_id, now - REQUEST_CLAIM_STALE_MS, now - REQUEST_CLAIM_STALE_MS);
6545
+ )`).run(ownerId, now, row.request_id, row.topic_id, now - REQUEST_CLAIM_STALE_MS, now - REQUEST_CLAIM_STALE_MS);
6526
6546
  if (Number(updated.changes ?? 0) === 0)
6527
6547
  return null;
6528
6548
  return rowToRequest({ ...row, claimed_by: ownerId, claimed_at: now });
@@ -6545,32 +6565,35 @@ function completeRuntimeUserTurnRequest(topicId, requestId) {
6545
6565
  return Number(result.changes ?? 0) > 0;
6546
6566
  }
6547
6567
  function getRuntimeUserTurnRequest(topicId) {
6548
- const row = db.query("SELECT * FROM runtime_user_turn_requests WHERE topic_id = ?").get(topicId);
6568
+ const row = db.query("SELECT * FROM runtime_user_turn_requests WHERE topic_id = ? ORDER BY created_at DESC, rowid DESC LIMIT 1").get(topicId);
6549
6569
  return row ? rowToRequest(row) : null;
6550
6570
  }
6551
- var REQUEST_CLAIM_STALE_MS;
6571
+ var REQUEST_CLAIM_STALE_MS, legacyTopicPrimaryKey;
6552
6572
  var init_runtime_turn_requests = __esm(async () => {
6553
6573
  await init_forum_db();
6554
6574
  await init_runtime_leases();
6555
6575
  await init_runtime_topic_state();
6556
6576
  REQUEST_CLAIM_STALE_MS = TURN_LEASE_STALE_MS;
6557
- db.exec(`
6558
- CREATE TABLE IF NOT EXISTS runtime_user_turn_requests (
6559
- topic_id TEXT PRIMARY KEY,
6560
- request_id TEXT NOT NULL UNIQUE,
6561
- user_id TEXT NOT NULL,
6562
- prompt TEXT NOT NULL,
6563
- attachments_json TEXT,
6564
- allow_auto_continue INTEGER NOT NULL DEFAULT 1 CHECK (allow_auto_continue IN (0, 1)),
6565
- execution_json TEXT,
6566
- topic_epoch INTEGER NOT NULL DEFAULT 0,
6567
- created_at INTEGER NOT NULL,
6568
- status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'running')),
6569
- claimed_by TEXT,
6570
- claimed_at INTEGER,
6571
- running_query_id TEXT
6572
- )
6573
- `);
6577
+ createRuntimeUserTurnRequestsTable();
6578
+ legacyTopicPrimaryKey = db.query("PRAGMA table_info(runtime_user_turn_requests)").all().some((column) => column.name === "topic_id" && column.pk === 1);
6579
+ if (legacyTopicPrimaryKey) {
6580
+ db.transaction(() => {
6581
+ db.exec("ALTER TABLE runtime_user_turn_requests RENAME TO runtime_user_turn_requests_legacy");
6582
+ createRuntimeUserTurnRequestsTable();
6583
+ db.exec(`
6584
+ INSERT INTO runtime_user_turn_requests (
6585
+ request_id, topic_id, user_id, prompt, attachments_json,
6586
+ allow_auto_continue, execution_json, topic_epoch, created_at,
6587
+ status, claimed_by, claimed_at, running_query_id
6588
+ )
6589
+ SELECT request_id, topic_id, user_id, prompt, attachments_json,
6590
+ allow_auto_continue, execution_json, topic_epoch, created_at,
6591
+ status, claimed_by, claimed_at, running_query_id
6592
+ FROM runtime_user_turn_requests_legacy
6593
+ `);
6594
+ db.exec("DROP TABLE runtime_user_turn_requests_legacy");
6595
+ })();
6596
+ }
6574
6597
  try {
6575
6598
  db.exec("ALTER TABLE runtime_user_turn_requests ADD COLUMN execution_json TEXT");
6576
6599
  } catch {}
@@ -11304,6 +11327,35 @@ await init_topic_cleanup();
11304
11327
 
11305
11328
  // ../../packages/core/src/application/execute-external-user-turn.ts
11306
11329
  await init_turn_runner();
11330
+ // ../../packages/core/src/application/submit-runtime-gateway-turn.ts
11331
+ await init_api_messages();
11332
+ await init_forum_db();
11333
+ await init_runtime_events();
11334
+
11335
+ // ../../packages/core/src/storage/runtime-gateway-submissions.ts
11336
+ await init_forum_db();
11337
+ db.exec(`
11338
+ CREATE TABLE IF NOT EXISTS runtime_gateway_submissions (
11339
+ client_message_id TEXT PRIMARY KEY,
11340
+ request_id TEXT NOT NULL UNIQUE,
11341
+ topic_id TEXT NOT NULL,
11342
+ message_id TEXT NOT NULL,
11343
+ user_id TEXT NOT NULL,
11344
+ created_at TEXT NOT NULL,
11345
+ ack_cursor INTEGER NOT NULL DEFAULT 0,
11346
+ message_cursor INTEGER NOT NULL DEFAULT 0
11347
+ )
11348
+ `);
11349
+ try {
11350
+ db.exec("ALTER TABLE runtime_gateway_submissions ADD COLUMN ack_cursor INTEGER NOT NULL DEFAULT 0");
11351
+ } catch {}
11352
+ try {
11353
+ db.exec("ALTER TABLE runtime_gateway_submissions ADD COLUMN message_cursor INTEGER NOT NULL DEFAULT 0");
11354
+ } catch {}
11355
+ db.exec("CREATE INDEX IF NOT EXISTS idx_runtime_gateway_submissions_topic ON runtime_gateway_submissions(topic_id)");
11356
+
11357
+ // ../../packages/core/src/application/submit-runtime-gateway-turn.ts
11358
+ await init_runtime_turn_requests();
11307
11359
  // ../../packages/core/src/application/submit-user-message.ts
11308
11360
  await init_bus();
11309
11361
  await init_turn_runner();
@@ -13225,4 +13277,4 @@ export {
13225
13277
  CRON_CONTEXT_RETAIN_TURNS
13226
13278
  };
13227
13279
 
13228
- //# debugId=A44DB415735252A864756E2164756E21
13280
+ //# debugId=53BCD0B037D08A3864756E2164756E21