@tangle-network/agent-app 0.60.249 → 0.60.250

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.
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Dispatch-first admission: start the producer, and so the model call, as soon
3
+ * as the turn's identity is known, while the lock, the history recheck, the
4
+ * user-row insert, `contextGate` and `beforeTurn` run. Nothing the producer
5
+ * emits reaches the member, the turn buffer or the transcript until that chain
6
+ * commits; a refusal aborts the producer and drops what it emitted.
7
+ *
8
+ * Why: on Creative production (2026-10-21, gemini-3.7-flash, n=32 Router
9
+ * turns) the chain between `authorize` and the producer held about 780 ms of
10
+ * every turn's p50 (admission 343, profile pin 338, turn open 99) before the
11
+ * model was even called. The model's own first token takes 2.5-3.6 s, so that
12
+ * work fits inside it.
13
+ *
14
+ * What stays fail-closed: `authorize` has already run, so no model call starts
15
+ * for a caller without identity and workspace access. Billing is enforced in
16
+ * `authorize` or at the model service, which holds spend before it calls the
17
+ * provider; the product names which (`dispatchFirst: { billing }`). Gates the
18
+ * model service cannot know, such as a seat, run in `contextGate` beside the
19
+ * model call, and the output waits for them. Single-flight is unchanged: the lock still decides which turn's output is
20
+ * kept; a refused duplicate costs only the model call it started, which its
21
+ * `admissionSignal` cancels.
22
+ */
23
+ import type { TurnTrace } from '../turn-trace/recorder';
24
+ import type { ChatTurnProduceArgs, ChatTurnRouteProducer, CreateChatTurnRoutesOptions } from './turn-route-contracts';
25
+ /** Thrown into `args.admitted` when the turn was refused after its producer started. */
26
+ export declare class TurnAdmissionRefusedError extends Error {
27
+ constructor();
28
+ }
29
+ export interface DispatchedProducer<TContext> {
30
+ /** The admission chain committed: hand the producer its final args and return it, with what it already emitted first. */
31
+ take(args: ChatTurnProduceArgs<TContext>): Promise<ChatTurnRouteProducer>;
32
+ /** The turn ended without the producer: abort it and discard its output. Idempotent; a no-op after `take`. */
33
+ refuse(): void;
34
+ }
35
+ /**
36
+ * Start `produce` now with the args known before the lock. `args.admitted`
37
+ * resolves to the final args at `take`, or rejects at `refuse`; the producer
38
+ * writes nothing durable before it resolves. Its stream is drained into memory
39
+ * from the start, so a producer that only works while pulled still dispatches.
40
+ */
41
+ export declare function dispatchProducerFirst<TContext>(produce: (args: ChatTurnProduceArgs<TContext>) => ChatTurnRouteProducer | Promise<ChatTurnRouteProducer>, args: ChatTurnProduceArgs<TContext>, trace: TurnTrace): DispatchedProducer<TContext>;
42
+ /** The dispatched producer for this turn when the route dispatches first and `when` picks it; otherwise none. */
43
+ export declare function dispatchFirstFor<TContext>(options: CreateChatTurnRoutesOptions<TContext>, args: ChatTurnProduceArgs<TContext>, trace: TurnTrace): DispatchedProducer<TContext> | undefined;
@@ -83,7 +83,7 @@ import {
83
83
  rowIdOf,
84
84
  storeSupportsDraftPersistence,
85
85
  streamChatRouteAsSandboxEvents
86
- } from "../chunk-BDCC6UVW.js";
86
+ } from "../chunk-A3KPDLSF.js";
87
87
  import "../chunk-TCGZFSN5.js";
88
88
  import "../chunk-FENBDSIW.js";
89
89
  import "../chunk-RN4WPTBH.js";
@@ -3,10 +3,12 @@ import { type AssistantDraftStore, type DraftPersistenceTuning } from './draft-p
3
3
  import { type AdmittedTurn, type TurnProgress } from './turn-admission';
4
4
  import type { ChatTurnRequestContext, ChatTurnTraceOptions, CreateChatTurnRoutesOptions } from './turn-route-contracts';
5
5
  import type { TurnRoutePhase } from './turn-progress';
6
- /** Work `afterAuthorize` started: `handOff` gives it to `produce`; `release` frees it if nothing took it. */
6
+ /** Work started before `produce` took the turn: `handOff` gives it to `produce`; `release` frees it if nothing took it. */
7
7
  export interface AfterAuthorizeWork {
8
8
  handOff(): void;
9
9
  release(): Promise<void>;
10
+ /** Free this too if the turn ends before `handOff`. */
11
+ add(release: () => void): void;
10
12
  }
11
13
  interface TurnExecutionOptions<TContext> {
12
14
  options: CreateChatTurnRoutesOptions<TContext>;
@@ -271,6 +271,15 @@ export interface ChatTurnProduceArgs<TContext> {
271
271
  * `createChatTurnRoutes` invocation supplies it before `produce` runs.
272
272
  */
273
273
  handoffCompletion?: () => Promise<void>;
274
+ /** Set only under `dispatchFirst`, where `produce` starts before the lock. Resolves to the final args (with
275
+ * `userMessageId`, after `contextGate` and `beforeTurn`) once the admission chain commits, or rejects with
276
+ * `TurnAdmissionRefusedError` when the turn is refused. Start the model call at once; write nothing durable,
277
+ * and read nothing `beforeTurn` adds to `context`, until it resolves. */
278
+ admitted?: Promise<ChatTurnProduceArgs<TContext>>;
279
+ /** Set only under `dispatchFirst`: aborts when the turn is refused after `produce` started, or when the route stops
280
+ * reading the producer (a disconnect under `cancelOnDisconnect`). Pass it to the model call. Under `dispatchFirst`,
281
+ * `handoffCompletion` resolves only after `admitted` does. */
282
+ admissionSignal?: AbortSignal;
274
283
  }
275
284
  /** One event as it crosses the route: the producer's own vocabulary, or an
276
285
  * injected keepalive. Same shape the engine forwards verbatim.
@@ -524,6 +533,28 @@ export interface CreateChatTurnRoutesOptions<TContext = void> {
524
533
  * wrapped in `createSandboxChatProducer`. Router/openai-compat lane: the
525
534
  * product's own producer. May be async (box resolution). */
526
535
  produce(args: ChatTurnProduceArgs<TContext>): ChatTurnRouteProducer | Promise<ChatTurnRouteProducer>;
536
+ /**
537
+ * Start `produce`, and so the model call, right after the history read, while the lock, the history recheck, the
538
+ * user-row insert, `contextGate` and `beforeTurn` run (see `./dispatch-first`). Nothing it emits reaches the member,
539
+ * the turn buffer or the transcript before that chain commits; a refusal aborts it through `args.admissionSignal`
540
+ * and drops its output. `produce` receives `args.admitted` for the final args.
541
+ *
542
+ * Opt in only by naming where billing is enforced before any provider is paid: `'authorize'` (the product's
543
+ * balance and spend checks run there) or `'model-service'` (the Tangle Router holds the spend and refuses an
544
+ * unfunded key before it calls a provider, fail-closed). Entitlement gates the model service cannot know (a seat,
545
+ * a plan feature) belong in `contextGate`, which then runs while the model starts and holds its output until it
546
+ * passes. `beforeTurn` may still mutate `context` but must not return a patch, since the prompt has already been
547
+ * sent; a patch throws. Omit → `produce` runs after admission.
548
+ *
549
+ * `when` picks the turns that dispatch first (default all). Return false for a producer whose dispatch is keyed by
550
+ * the turn's `executionId` (a box session): two sends on one thread resolve the same id, and the refused duplicate
551
+ * would reach the winner's execution before the lock refused it. A Router call per request is safe; the refused
552
+ * duplicate costs only the call its `admissionSignal` cancels.
553
+ */
554
+ dispatchFirst?: {
555
+ billing: 'authorize' | 'model-service';
556
+ when?: (args: ChatTurnProduceArgs<TContext>) => boolean;
557
+ };
527
558
  /** Single-flight lock acquired before any side effect and released once when
528
559
  * the turn settles (including short-circuit/throw). `/turn-stream`'s
529
560
  * `createDurableTurnLock` is the shared DO-backed implementation. Omit →
@@ -289,6 +289,109 @@ function userPartsWithFiles(userParts, fileParts, mentions) {
289
289
  // src/chat-routes/turn-execution.ts
290
290
  import { deriveExecutionId, handleChatTurn } from "@tangle-network/agent-runtime/durable";
291
291
 
292
+ // src/chat-routes/dispatch-first.ts
293
+ var TurnAdmissionRefusedError = class extends Error {
294
+ constructor() {
295
+ super("The turn was refused after its producer started; its output was discarded.");
296
+ this.name = "TurnAdmissionRefusedError";
297
+ }
298
+ };
299
+ function dispatchProducerFirst(produce, args, trace) {
300
+ const abort = new AbortController();
301
+ let admit;
302
+ let reject;
303
+ const admitted = new Promise((resolve, fail) => {
304
+ admit = resolve;
305
+ reject = fail;
306
+ });
307
+ admitted.catch(() => {
308
+ });
309
+ let settled;
310
+ let stopped = false;
311
+ const queue = [];
312
+ let ended = false;
313
+ let failure;
314
+ let wake;
315
+ const notify = () => {
316
+ const resume = wake;
317
+ wake = void 0;
318
+ resume?.();
319
+ };
320
+ let source;
321
+ const handoffCompletion = args.handoffCompletion;
322
+ const started = trace.measure("admission.produce", "admission", () => produce({
323
+ ...args,
324
+ admitted,
325
+ admissionSignal: abort.signal,
326
+ ...handoffCompletion ? { handoffCompletion: () => admitted.then(() => handoffCompletion()) } : {}
327
+ }));
328
+ const draining = started.then(async (producer) => {
329
+ try {
330
+ source = producer.stream[Symbol.asyncIterator]();
331
+ for (; ; ) {
332
+ const next = await source.next();
333
+ if (next.done || stopped) break;
334
+ queue.push(next.value);
335
+ notify();
336
+ }
337
+ } catch (error) {
338
+ if (!stopped) failure = { error };
339
+ } finally {
340
+ ended = true;
341
+ notify();
342
+ }
343
+ }, (error) => {
344
+ failure = { error };
345
+ ended = true;
346
+ notify();
347
+ });
348
+ const stop = (reason) => {
349
+ if (stopped) return;
350
+ stopped = true;
351
+ abort.abort(reason);
352
+ queue.length = 0;
353
+ notify();
354
+ void started.then(() => source?.return?.(), () => {
355
+ }).catch(() => {
356
+ });
357
+ };
358
+ async function* replay() {
359
+ try {
360
+ for (; ; ) {
361
+ while (queue.length) yield queue.shift();
362
+ if (ended) break;
363
+ await new Promise((resolve) => {
364
+ wake = resolve;
365
+ });
366
+ }
367
+ await draining;
368
+ if (failure) throw failure.error;
369
+ } finally {
370
+ if (!ended) stop(new Error("The turn stopped reading its producer."));
371
+ }
372
+ }
373
+ return {
374
+ async take(final) {
375
+ if (settled) throw new Error(`dispatch-first producer already ${settled}`);
376
+ settled = "taken";
377
+ admit(final);
378
+ const producer = await started;
379
+ return Object.defineProperty(Object.create(producer), "stream", { value: replay(), enumerable: true });
380
+ },
381
+ refuse() {
382
+ if (settled) return;
383
+ settled = "refused";
384
+ reject(new TurnAdmissionRefusedError());
385
+ stop(new TurnAdmissionRefusedError());
386
+ }
387
+ };
388
+ }
389
+ function dispatchFirstFor(options, args, trace) {
390
+ const policy = options.dispatchFirst;
391
+ if (!policy || !(policy.when?.(args) ?? true)) return void 0;
392
+ return dispatchProducerFirst(options.produce, args, trace);
393
+ }
394
+
292
395
  // src/chat-routes/turn-event-stream.ts
293
396
  function failureReasonOf(data) {
294
397
  if (!data) return void 0;
@@ -460,6 +563,14 @@ async function closeIteratorAfterAbort(iterator) {
460
563
  }
461
564
 
462
565
  // src/chat-routes/turn-execution.ts
566
+ async function readTurnHistory(store, threadId) {
567
+ return (await store.listMessages(threadId)).map((m) => ({
568
+ id: m.id,
569
+ role: m.role,
570
+ content: m.content,
571
+ parts: m.parts ?? null
572
+ }));
573
+ }
463
574
  function modelAttributionFields(attribution) {
464
575
  return {
465
576
  ...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
@@ -508,12 +619,7 @@ function createAdmittedTurnRunner({
508
619
  }));
509
620
  if (!prepared.ok) return prepared.response;
510
621
  }
511
- const readHistory = async () => (await options.store.listMessages(payload.threadId)).map((m) => ({
512
- id: m.id,
513
- role: m.role,
514
- content: m.content,
515
- parts: m.parts ?? null
516
- }));
622
+ const readHistory = () => readTurnHistory(options.store, payload.threadId);
517
623
  const historySpan = trace.start("admission.history", "admission");
518
624
  const existingMessages = await readHistory();
519
625
  const historyBeforeLock = options.turnLock ? JSON.stringify(existingMessages) : void 0;
@@ -585,6 +691,8 @@ function createAdmittedTurnRunner({
585
691
  ...intentLine ? { intentLine } : {},
586
692
  ...ctx?.executionLimits ? { executionLimits: ctx.executionLimits } : {}
587
693
  };
694
+ const dispatched = dispatchFirstFor(options, produceArgs, trace);
695
+ if (dispatched) early.add(dispatched.refuse);
588
696
  let lockAcquired = false;
589
697
  let lockHandle;
590
698
  let lockReleased = false;
@@ -808,6 +916,7 @@ function createAdmittedTurnRunner({
808
916
  const beforeTurn = options.beforeTurn;
809
917
  if (beforeTurn) {
810
918
  const patch = await trace.measure("admission.before_turn", "admission", (span) => beforeTurn(produceArgs, { span }));
919
+ if (patch && Object.keys(patch).length && dispatched) throw new Error("beforeTurn cannot patch a dispatchFirst turn: its prompt is already sent");
811
920
  if (patch) produceArgs = { ...produceArgs, ...patch };
812
921
  }
813
922
  const streamSetup = trace.start("admission.stream_setup", "admission");
@@ -870,7 +979,7 @@ function createAdmittedTurnRunner({
870
979
  // defer it into the generator's first pull.
871
980
  produce: () => ({
872
981
  stream: (async function* () {
873
- producer = await trace.measure("admission.produce", "admission", () => options.produce(produceArgs));
982
+ producer = await (dispatched ? dispatched.take(produceArgs) : trace.measure("admission.produce", "admission", () => options.produce(produceArgs)));
874
983
  let source = producer.stream;
875
984
  if (ctx?.cancelOnDisconnect) {
876
985
  source = abortOnSignal(source, request.signal);
@@ -1051,6 +1160,32 @@ function createAdmittedTurnRunner({
1051
1160
  }
1052
1161
 
1053
1162
  // src/chat-routes/turn-routes.ts
1163
+ function earlyTurnWork(release, log) {
1164
+ let free = release;
1165
+ let also = [];
1166
+ return {
1167
+ handOff: () => {
1168
+ free = void 0;
1169
+ also = [];
1170
+ },
1171
+ add: (more) => {
1172
+ also.push(more);
1173
+ },
1174
+ release: async () => {
1175
+ const product = free;
1176
+ const route = also;
1177
+ free = void 0;
1178
+ also = [];
1179
+ for (const stop of route) stop();
1180
+ if (!product) return;
1181
+ try {
1182
+ await product();
1183
+ } catch (err) {
1184
+ log("[chat-routes] afterAuthorize release failed", { error: err instanceof Error ? err.message : String(err) });
1185
+ }
1186
+ }
1187
+ };
1188
+ }
1054
1189
  function createChatTurnRoutes(options) {
1055
1190
  const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
1056
1191
  const draftStore = options.store;
@@ -1241,21 +1376,7 @@ function createChatTurnRoutes(options) {
1241
1376
  log("[chat-routes] afterAuthorize failed; the turn goes on without it", { error: err instanceof Error ? err.message : String(err) });
1242
1377
  }
1243
1378
  }
1244
- return {
1245
- handOff: () => {
1246
- release = void 0;
1247
- },
1248
- release: async () => {
1249
- const free = release;
1250
- release = void 0;
1251
- if (!free) return;
1252
- try {
1253
- await free();
1254
- } catch (err) {
1255
- log("[chat-routes] afterAuthorize release failed", { error: err instanceof Error ? err.message : String(err) });
1256
- }
1257
- }
1258
- };
1379
+ return earlyTurnWork(release, log);
1259
1380
  }
1260
1381
  async function runTurn(request, ctx, admitted, progress, startedIntent) {
1261
1382
  const early = startAfterAuthorize(request, admitted);
@@ -1513,4 +1634,4 @@ export {
1513
1634
  createChatTurnRoutes,
1514
1635
  streamChatRouteAsSandboxEvents
1515
1636
  };
1516
- //# sourceMappingURL=chunk-BDCC6UVW.js.map
1637
+ //# sourceMappingURL=chunk-A3KPDLSF.js.map