@tangle-network/agent-app 4.0.6 → 4.0.7

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.
Files changed (32) hide show
  1. package/dist/billing/index.js +1 -1
  2. package/dist/chat-routes/index.js +5 -5
  3. package/dist/chat-routes/route-storage.d.ts +1 -0
  4. package/dist/chat-routes/turn-ownership.d.ts +7 -3
  5. package/dist/chat-routes/turn-route-contracts.d.ts +6 -1
  6. package/dist/{chunk-ROTSZSYZ.js → chunk-4OR7NNYB.js} +49 -11
  7. package/dist/chunk-4OR7NNYB.js.map +1 -0
  8. package/dist/{chunk-JFBJ7MGK.js → chunk-ASWAPIJM.js} +40 -3
  9. package/dist/chunk-ASWAPIJM.js.map +1 -0
  10. package/dist/{chunk-IYLUPXYS.js → chunk-FTGKXM7O.js} +3 -4
  11. package/dist/chunk-FTGKXM7O.js.map +1 -0
  12. package/dist/{chunk-ZTGITMYQ.js → chunk-FZOHBYGV.js} +74 -13
  13. package/dist/chunk-FZOHBYGV.js.map +1 -0
  14. package/dist/{chunk-2PCNHHN2.js → chunk-WGT32NQ4.js} +2 -2
  15. package/dist/{chunk-3Q3UEWKK.js → chunk-YQE2CGIO.js} +1 -1
  16. package/dist/{chunk-3Q3UEWKK.js.map → chunk-YQE2CGIO.js.map} +1 -1
  17. package/dist/preset-cloudflare/index.js +4 -4
  18. package/dist/public-consultation/index.js +5 -5
  19. package/dist/sandbox/holder-box.d.ts +13 -0
  20. package/dist/sandbox/index.d.ts +1 -0
  21. package/dist/sandbox/index.js +7 -3
  22. package/dist/stream/index.js +2 -2
  23. package/dist/stream/turn-buffer.d.ts +5 -2
  24. package/dist/turn-stream/adapters.d.ts +42 -1
  25. package/dist/turn-stream/index.js +4 -2
  26. package/dist/turn-stream/index.js.map +1 -1
  27. package/package.json +1 -1
  28. package/dist/chunk-IYLUPXYS.js.map +0 -1
  29. package/dist/chunk-JFBJ7MGK.js.map +0 -1
  30. package/dist/chunk-ROTSZSYZ.js.map +0 -1
  31. package/dist/chunk-ZTGITMYQ.js.map +0 -1
  32. /package/dist/{chunk-2PCNHHN2.js.map → chunk-WGT32NQ4.js.map} +0 -0
@@ -4,7 +4,7 @@ import {
4
4
  createTcloudKeyProvisioner,
5
5
  createWorkspaceKeyManager,
6
6
  readSharedBillingState
7
- } from "../chunk-IYLUPXYS.js";
7
+ } from "../chunk-FTGKXM7O.js";
8
8
  import "../chunk-VGS2NXLB.js";
9
9
  export {
10
10
  createIdentityBoundWorkspaceKeyManager,
@@ -88,14 +88,14 @@ import {
88
88
  turnPromptText,
89
89
  valueRedactor,
90
90
  withProducerStream
91
- } from "../chunk-ROTSZSYZ.js";
91
+ } from "../chunk-4OR7NNYB.js";
92
92
  import "../chunk-SBHFMDR7.js";
93
93
  import "../chunk-H6BRJU5M.js";
94
94
  import "../chunk-4DBHC63F.js";
95
95
  import "../chunk-RN4WPTBH.js";
96
96
  import "../chunk-MM6RLOXF.js";
97
97
  import "../chunk-M4QITAQV.js";
98
- import "../chunk-JFBJ7MGK.js";
98
+ import "../chunk-ASWAPIJM.js";
99
99
  import "../chunk-F4TUZYMX.js";
100
100
  import {
101
101
  redactErrorMessage
@@ -110,7 +110,7 @@ import {
110
110
  storeSupportsDraftPersistence,
111
111
  transformVisibleTextParts
112
112
  } from "../chunk-K4YIDNX5.js";
113
- import "../chunk-2PCNHHN2.js";
113
+ import "../chunk-WGT32NQ4.js";
114
114
  import "../chunk-YG66IHDU.js";
115
115
  import {
116
116
  attachmentInputToPart,
@@ -129,11 +129,11 @@ import {
129
129
  DEFAULT_STALE_TURN_LOCK_GRACE_MS,
130
130
  DEFAULT_TERMINAL_TURN_LOCK_GRACE_MS,
131
131
  reconcileStaleTurnLock
132
- } from "../chunk-ZTGITMYQ.js";
132
+ } from "../chunk-FZOHBYGV.js";
133
133
  import {
134
134
  coalesceDeltas,
135
135
  createBufferedTurnTap
136
- } from "../chunk-3Q3UEWKK.js";
136
+ } from "../chunk-YQE2CGIO.js";
137
137
  import {
138
138
  AGENT_HANDLE_PATTERN,
139
139
  ChatTurnInputError,
@@ -17,6 +17,7 @@ export declare function createDefaultTurnLock<TContext>(store: Pick<ChatTurnStor
17
17
  acquire(args: import("../turn-stream/index.js").TurnLockSeamArgs<TContext>): Promise<import("../turn-stream/index.js").TurnLockSeamResult>;
18
18
  release(handle: unknown): Promise<void>;
19
19
  park(handle: unknown): Promise<boolean>;
20
+ holdForHandoff?(handle: unknown): Promise<boolean>;
20
21
  warm(target: {
21
22
  tenantId: string;
22
23
  threadId?: string;
@@ -1,6 +1,6 @@
1
1
  import type { AssistantDraftWriter } from './draft-persistence.js';
2
2
  import type { NativeCompletionHandoffOptions, NativeTimingTransfer } from './native-turn-timing.js';
3
- import type { BufferedTurnTap } from '../stream/index.js';
3
+ import { type BufferedTurnTap, type TurnEventStore } from '../stream/index.js';
4
4
  import type { ChatTurnLock, ChatTurnRouteProducer } from './turn-route-contracts.js';
5
5
  interface OwnershipState {
6
6
  producer?: ChatTurnRouteProducer;
@@ -16,9 +16,13 @@ interface OwnershipState {
16
16
  }
17
17
  /** One transfer of terminal authority, retaining the route only as a live projector. */
18
18
  export declare function createChatTurnOwnership(state: OwnershipState, log: (message: string, meta?: Record<string, unknown>) => void, turnStreamId: string, options: {
19
- turnLock?: Pick<ChatTurnLock<unknown>, 'release' | 'park'>;
20
- }): {
19
+ turnLock?: Pick<ChatTurnLock<unknown>, 'release' | 'park' | 'holdForHandoff'>;
20
+ turnStore?: Pick<TurnEventStore, 'setStatus'>;
21
+ }, scopeId?: string): {
21
22
  handoffCompletion: (handoff?: NativeCompletionHandoffOptions) => Promise<void>;
22
23
  releaseLock: () => Promise<void>;
24
+ markRunning: () => Promise<void>;
25
+ stopRenewal: () => Promise<void>;
26
+ releaseBeforeDrain: (end: 'complete' | 'error' | 'handoff') => Promise<void>;
23
27
  };
24
28
  export {};
@@ -425,6 +425,9 @@ export interface ChatTurnLock<TContext, TResource = unknown> {
425
425
  release(handle: unknown): void | Promise<void>;
426
426
  /** Record a release for the durable owner to complete without freeing the lock; absent or false, the lock stays held. */
427
427
  park?(handle: unknown): Promise<boolean>;
428
+ /** Keep the lock held for the durable owner a turn hands off to (`handoffCompletion`), for a lock whose stale-lock
429
+ * reclaim would read the turn as dead once the route stops renewing it. The owner's release still frees it. */
430
+ holdForHandoff?(handle: unknown): Promise<boolean>;
428
431
  /** Bring up whatever `acquire` will talk to, as soon as the turn is admitted
429
432
  * and while `prepareTurn` and the history read run. Never awaited; a failure
430
433
  * is logged and the turn goes on to `acquire` as usual. */
@@ -678,7 +681,9 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TResource = unknow
678
681
  * - `{proceed:'handoff'}` releases the route-acquired lock, returns the
679
682
  * product's accepted `Response`, and transfers execution and lifecycle
680
683
  * ownership to the product's durable owner. The route runs no producer
681
- * or lifecycle hook.
684
+ * or lifecycle hook. With a lock that uses the default reclaim, the route
685
+ * wrote a running row for `args.turnStreamId` at acquire; the owner
686
+ * settles it (or it lapses with its lease).
682
687
  * - Always returning `{proceed:true}` is supported, not a misuse: the seam
683
688
  * doubles as the one place that runs after the user row exists and before
684
689
  * the producer, which is where per-turn analytics and readiness
@@ -31,7 +31,7 @@ import {
31
31
  sandboxRunFailureDetail,
32
32
  streamSandboxPrompt,
33
33
  turnStorageBindings
34
- } from "./chunk-JFBJ7MGK.js";
34
+ } from "./chunk-ASWAPIJM.js";
35
35
  import {
36
36
  redactErrorMessage
37
37
  } from "./chunk-MH74DY2I.js";
@@ -79,11 +79,12 @@ import {
79
79
  parseJsonObjectBody
80
80
  } from "./chunk-ALIWB2P3.js";
81
81
  import {
82
+ DEFAULT_RUNNING_TURN_RENEW_INTERVAL_MS,
82
83
  coalesceDeltas,
83
84
  createBufferedTurnTap,
84
85
  replayTurnEvents,
85
86
  stampReplaySeq
86
- } from "./chunk-3Q3UEWKK.js";
87
+ } from "./chunk-YQE2CGIO.js";
87
88
  import {
88
89
  ChatTurnInputError,
89
90
  DISPATCH_MAX_PARTS,
@@ -2506,7 +2507,7 @@ function createNativeTimingTransfer(read, onError) {
2506
2507
  }
2507
2508
 
2508
2509
  // src/chat-routes/turn-ownership.ts
2509
- function createChatTurnOwnership(state, log, turnStreamId, options) {
2510
+ function createChatTurnOwnership(state, log, turnStreamId, options, scopeId) {
2510
2511
  const attempt = async (step, run) => {
2511
2512
  try {
2512
2513
  await run();
@@ -2514,10 +2515,37 @@ function createChatTurnOwnership(state, log, turnStreamId, options) {
2514
2515
  log(`[chat-routes] ${step} failed`, { turnId: turnStreamId, error: err instanceof Error ? err.message : String(err) });
2515
2516
  }
2516
2517
  };
2518
+ const store = options.turnLock?.holdForHandoff ? options.turnStore : void 0;
2519
+ let runningRow;
2520
+ let renewal;
2521
+ const markRunning = async () => {
2522
+ if (!store) return;
2523
+ const write = () => runningRow = (runningRow ?? Promise.resolve()).catch(() => {
2524
+ }).then(() => store.setStatus(turnStreamId, "running", scopeId));
2525
+ const first = write();
2526
+ renewal = setInterval(() => void write().catch(() => {
2527
+ }), DEFAULT_RUNNING_TURN_RENEW_INTERVAL_MS);
2528
+ return first;
2529
+ };
2530
+ const stopRenewal = async () => {
2531
+ clearInterval(renewal);
2532
+ renewal = void 0;
2533
+ await runningRow?.catch(() => {
2534
+ });
2535
+ };
2517
2536
  const handoffCompletion = (handoff) => {
2518
2537
  if (!state.completionHandoffPromise) {
2519
2538
  state.completionHandoffPromise = (async () => {
2520
2539
  state.completionHandoff = true;
2540
+ await stopRenewal();
2541
+ if (state.lockAcquired && options.turnLock?.holdForHandoff) {
2542
+ let held;
2543
+ for (let tries = 0; tries < 3 && held === void 0; tries++) {
2544
+ if (tries) await new Promise((resolve) => setTimeout(resolve, 250 * tries));
2545
+ held = await Promise.resolve().then(() => options.turnLock.holdForHandoff(state.lockHandle)).then(Boolean, () => void 0);
2546
+ }
2547
+ if (!held) log("[chat-routes] turn lock hand-off hold failed", { turnId: turnStreamId, class: "turn_lock_handoff_hold_failed" });
2548
+ }
2521
2549
  let registrationFailed = false;
2522
2550
  try {
2523
2551
  await state.nativeTiming?.register(handoff);
@@ -2553,7 +2581,15 @@ function createChatTurnOwnership(state, log, turnStreamId, options) {
2553
2581
  await attempt("turn cancellation close", () => state.closeCancellation?.());
2554
2582
  await attempt("turnLock.release", () => options.turnLock.release(state.lockHandle));
2555
2583
  };
2556
- return { handoffCompletion, releaseLock };
2584
+ const releaseBeforeDrain = async (end) => {
2585
+ await stopRenewal();
2586
+ if (end !== "handoff" && !state.completionHandoff) {
2587
+ if (state.tap) await attempt("turn buffer close", () => state.tap.done(end));
2588
+ else if (store && runningRow) await attempt("running row settle", () => store.setStatus(turnStreamId, end, scopeId));
2589
+ }
2590
+ await releaseLock();
2591
+ };
2592
+ return { handoffCompletion, releaseLock, markRunning, stopRenewal, releaseBeforeDrain };
2557
2593
  }
2558
2594
 
2559
2595
  // src/chat-routes/route-producer.ts
@@ -2947,7 +2983,7 @@ async function runAdmittedTurn({
2947
2983
  turnId: turnStreamId,
2948
2984
  error: error instanceof Error ? error.message : String(error)
2949
2985
  }));
2950
- const { handoffCompletion, releaseLock } = createChatTurnOwnership(state, log, turnStreamId, options);
2986
+ const { handoffCompletion, releaseLock, markRunning, stopRenewal, releaseBeforeDrain } = createChatTurnOwnership(state, log, turnStreamId, options, payload.threadId);
2951
2987
  let produceArgs = {
2952
2988
  lock: () => state.lockHandle,
2953
2989
  request,
@@ -2999,8 +3035,9 @@ async function runAdmittedTurn({
2999
3035
  progress?.("preparing", progressMessages.preparing);
3000
3036
  const { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle } = createTurnSettlement(state, trace, options, executionId, traceSink, identity, traceOptions, context, turnStreamId, log, ctx, () => produceArgs.userMessageId);
3001
3037
  try {
3002
- if (state.lockAcquired && JSON.stringify(await trace.measure("admission.history_recheck", "admission", readHistory)) !== historyBeforeLock) {
3003
- await releaseLock();
3038
+ const [history] = state.lockAcquired ? await trace.measure("admission.history_recheck", "admission", () => Promise.all([readHistory(), markRunning()])) : [];
3039
+ if (state.lockAcquired && JSON.stringify(history) !== historyBeforeLock) {
3040
+ await releaseBeforeDrain("error");
3004
3041
  return Response.json({
3005
3042
  code: "chat_history_changed",
3006
3043
  error: "The conversation changed while this turn was acquiring its lock. Refresh the conversation before starting new work."
@@ -3025,14 +3062,14 @@ async function runAdmittedTurn({
3025
3062
  if (contextGate) {
3026
3063
  const gate = await trace.measure("admission.gate", "admission", () => contextGate(produceArgs));
3027
3064
  if (gate.proceed === "handoff") {
3028
- await releaseLock();
3065
+ await releaseBeforeDrain("handoff");
3029
3066
  state.legacyHandoff = true;
3030
3067
  endTrace(false, void 0);
3031
3068
  exportTraceInBackground();
3032
3069
  return gate.response;
3033
3070
  }
3034
3071
  if (!gate.proceed) {
3035
- await releaseLock();
3072
+ await releaseBeforeDrain("complete");
3036
3073
  if (state.completionHandoff) {
3037
3074
  endTrace(false, void 0);
3038
3075
  exportTraceInBackground();
@@ -3078,6 +3115,7 @@ async function runAdmittedTurn({
3078
3115
  });
3079
3116
  const turnMarker = { type: "turn", turnId: turnStreamId };
3080
3117
  await state.tap.onEvent(turnMarker);
3118
+ await stopRenewal();
3081
3119
  if (state.completionHandoff) await state.tap.detach();
3082
3120
  if (draftTuning) {
3083
3121
  state.draft = createAssistantDraftWriter({
@@ -3128,7 +3166,7 @@ async function runAdmittedTurn({
3128
3166
  } catch (err) {
3129
3167
  if (!state.completionHandoff) {
3130
3168
  if (state.turnStarted) await fireTerminalLifecycle(true, err);
3131
- await releaseLock();
3169
+ await releaseBeforeDrain("error");
3132
3170
  }
3133
3171
  endTrace(true, err);
3134
3172
  exportTraceInBackground();
@@ -4232,4 +4270,4 @@ export {
4232
4270
  createChatTurnRoutes,
4233
4271
  streamChatRouteAsSandboxEvents
4234
4272
  };
4235
- //# sourceMappingURL=chunk-ROTSZSYZ.js.map
4273
+ //# sourceMappingURL=chunk-4OR7NNYB.js.map