@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.mjs CHANGED
@@ -2782,6 +2782,52 @@ import {
2782
2782
 
2783
2783
  // src/services/threads/threadOwnership.ts
2784
2784
  import { getStoreLattice as getStoreLattice5 } from "@axiom-lattice/core";
2785
+ var DELEGATED_THREAD_SEPARATOR = "____";
2786
+ var MAX_DELEGATED_THREAD_LINEAGE = 8;
2787
+ async function threadHasTenantRecord(tenantId, threadId) {
2788
+ try {
2789
+ const thread = await getStoreLattice5("default", "thread").store.getThreadById(
2790
+ tenantId,
2791
+ threadId
2792
+ );
2793
+ if (thread) return true;
2794
+ } catch {
2795
+ }
2796
+ try {
2797
+ const ledger = getStoreLattice5("default", "agentRun").store;
2798
+ const runs = await ledger.listRuns(tenantId, threadId, { limit: 1 });
2799
+ if (runs.length > 0) return true;
2800
+ } catch {
2801
+ }
2802
+ try {
2803
+ const tasks = getStoreLattice5("default", "task").store;
2804
+ const matches = await tasks.list({ tenantId, threadId, limit: 1 });
2805
+ return matches.length > 0;
2806
+ } catch {
2807
+ return false;
2808
+ }
2809
+ }
2810
+ async function threadLineageRootedInTenant(tenantId, threadId) {
2811
+ let candidate = threadId;
2812
+ for (let depth = 0; depth < MAX_DELEGATED_THREAD_LINEAGE; depth += 1) {
2813
+ if (await threadHasTenantRecord(tenantId, candidate)) return true;
2814
+ const boundary = candidate.lastIndexOf(DELEGATED_THREAD_SEPARATOR);
2815
+ if (boundary <= 0) return false;
2816
+ candidate = candidate.slice(0, boundary);
2817
+ }
2818
+ return false;
2819
+ }
2820
+ async function delegatedThreadOwnedBy(tenantId, assistantId, threadId) {
2821
+ const marker = `${DELEGATED_THREAD_SEPARATOR}${assistantId}_`;
2822
+ let boundary = threadId.indexOf(marker);
2823
+ while (boundary > 0) {
2824
+ if (await threadLineageRootedInTenant(tenantId, threadId.slice(0, boundary))) {
2825
+ return true;
2826
+ }
2827
+ boundary = threadId.indexOf(marker, boundary + DELEGATED_THREAD_SEPARATOR.length);
2828
+ }
2829
+ return false;
2830
+ }
2785
2831
  async function threadOwnedBy(tenantId, assistantId, threadId) {
2786
2832
  try {
2787
2833
  const thread = await getStoreLattice5("default", "thread").store.getThreadById(
@@ -2800,10 +2846,12 @@ async function threadOwnedBy(tenantId, assistantId, threadId) {
2800
2846
  try {
2801
2847
  const tasks = getStoreLattice5("default", "task").store;
2802
2848
  const matches = await tasks.list({ tenantId, threadId, limit: 1 });
2803
- return matches.some((task) => task.ownerType === "agent" && task.ownerId === assistantId);
2849
+ if (matches.some((task) => task.ownerType === "agent" && task.ownerId === assistantId)) {
2850
+ return true;
2851
+ }
2804
2852
  } catch {
2805
- return false;
2806
2853
  }
2854
+ return delegatedThreadOwnedBy(tenantId, assistantId, threadId);
2807
2855
  }
2808
2856
  async function threadIsNewForTenant(tenantId, threadId) {
2809
2857
  try {