@axiom-lattice/gateway 8.1.0 → 8.1.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/index.js CHANGED
@@ -11421,6 +11421,52 @@ var import_core8 = require("@axiom-lattice/core");
11421
11421
 
11422
11422
  // src/services/threads/threadOwnership.ts
11423
11423
  var import_core7 = require("@axiom-lattice/core");
11424
+ var DELEGATED_THREAD_SEPARATOR = "____";
11425
+ var MAX_DELEGATED_THREAD_LINEAGE = 8;
11426
+ async function threadHasTenantRecord(tenantId, threadId) {
11427
+ try {
11428
+ const thread = await (0, import_core7.getStoreLattice)("default", "thread").store.getThreadById(
11429
+ tenantId,
11430
+ threadId
11431
+ );
11432
+ if (thread) return true;
11433
+ } catch {
11434
+ }
11435
+ try {
11436
+ const ledger = (0, import_core7.getStoreLattice)("default", "agentRun").store;
11437
+ const runs = await ledger.listRuns(tenantId, threadId, { limit: 1 });
11438
+ if (runs.length > 0) return true;
11439
+ } catch {
11440
+ }
11441
+ try {
11442
+ const tasks = (0, import_core7.getStoreLattice)("default", "task").store;
11443
+ const matches = await tasks.list({ tenantId, threadId, limit: 1 });
11444
+ return matches.length > 0;
11445
+ } catch {
11446
+ return false;
11447
+ }
11448
+ }
11449
+ async function threadLineageRootedInTenant(tenantId, threadId) {
11450
+ let candidate = threadId;
11451
+ for (let depth = 0; depth < MAX_DELEGATED_THREAD_LINEAGE; depth += 1) {
11452
+ if (await threadHasTenantRecord(tenantId, candidate)) return true;
11453
+ const boundary = candidate.lastIndexOf(DELEGATED_THREAD_SEPARATOR);
11454
+ if (boundary <= 0) return false;
11455
+ candidate = candidate.slice(0, boundary);
11456
+ }
11457
+ return false;
11458
+ }
11459
+ async function delegatedThreadOwnedBy(tenantId, assistantId, threadId) {
11460
+ const marker = `${DELEGATED_THREAD_SEPARATOR}${assistantId}_`;
11461
+ let boundary = threadId.indexOf(marker);
11462
+ while (boundary > 0) {
11463
+ if (await threadLineageRootedInTenant(tenantId, threadId.slice(0, boundary))) {
11464
+ return true;
11465
+ }
11466
+ boundary = threadId.indexOf(marker, boundary + DELEGATED_THREAD_SEPARATOR.length);
11467
+ }
11468
+ return false;
11469
+ }
11424
11470
  async function threadOwnedBy(tenantId, assistantId, threadId) {
11425
11471
  try {
11426
11472
  const thread = await (0, import_core7.getStoreLattice)("default", "thread").store.getThreadById(
@@ -11439,10 +11485,12 @@ async function threadOwnedBy(tenantId, assistantId, threadId) {
11439
11485
  try {
11440
11486
  const tasks = (0, import_core7.getStoreLattice)("default", "task").store;
11441
11487
  const matches = await tasks.list({ tenantId, threadId, limit: 1 });
11442
- return matches.some((task) => task.ownerType === "agent" && task.ownerId === assistantId);
11488
+ if (matches.some((task) => task.ownerType === "agent" && task.ownerId === assistantId)) {
11489
+ return true;
11490
+ }
11443
11491
  } catch {
11444
- return false;
11445
11492
  }
11493
+ return delegatedThreadOwnedBy(tenantId, assistantId, threadId);
11446
11494
  }
11447
11495
  async function threadIsNewForTenant(tenantId, threadId) {
11448
11496
  try {