opencode-zen-freex 0.2.16 → 0.2.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/tool-response.mjs CHANGED
@@ -71,10 +71,14 @@ function foldsTo(declared, key) {
71
71
  function declaredNames(policy) {
72
72
  const held = policy?.declared;
73
73
  const names = held instanceof Set ? [...held] : held;
74
+ // A field that is not one of the two shapes gets here as itself: `names` is
75
+ // the field's own value unless it was a Set, so an array never reaches this
76
+ // message (the old `Array.isArray(held) ? "an array of those"` arm could not
77
+ // be read at all).
74
78
  if (!Array.isArray(names))
75
79
  throw new TypeError(
76
80
  `policy.declared must be a Set or an array of tool names, not ${
77
- held === null ? "null" : Array.isArray(held) ? "an array of those" : typeof held
81
+ held === null ? "null" : typeof held
78
82
  }`,
79
83
  );
80
84
  for (const name of names) {
@@ -182,6 +186,16 @@ function isCall(protocol, value) {
182
186
  );
183
187
  }
184
188
 
189
+ // What a call is named by, when it is named at all: a non-empty string. ""
190
+ // names nothing a client can key by — the empty id this route was measured
191
+ // putting on a call — and neither does a number or a boolean. Every place
192
+ // that asks whether a call has an identity asks it here, or two calls of one
193
+ // reply share one item (measured: two `"call_id": ""` calls each became the
194
+ // item `msg_`, and their arguments frames could not be matched to either).
195
+ function identity(value) {
196
+ return typeof value === "string" && value !== "" ? value : undefined;
197
+ }
198
+
185
199
  // Counts the ids invented for calls the reply gives nothing to place: a
186
200
  // counter cannot be stable, but it is the only thing that keeps two of them
187
201
  // apart (see inPlaceOf).
@@ -190,10 +204,10 @@ let synthesized = 0;
190
204
  // What a call left out of a Responses or Messages reply becomes: text
191
205
  // saying so, in the call's place, so no later index moves. `at` is where the
192
206
  // call sits in the reply as far as the caller can tell — a frame's
193
- // output_index, or the index the walk found it at. `placed` is the token a
194
- // caller that follows the whole stream already settled on for a call whose
195
- // frames place it nowhere at all (see snapshotStream): without it every
196
- // rewrite of that call invented a new id.
207
+ // output_index, or the index the walk found it at. `placed` is the id a
208
+ // caller that follows the whole stream has already answered for this call
209
+ // (see snapshotStream): without it every rewrite of that call invented a new
210
+ // id.
197
211
  function inPlaceOf(protocol, call, note, at, placed) {
198
212
  if (protocol === "anthropic") return { type: "text", text: note };
199
213
  // An item id is what a Responses client keys the item by, and what the
@@ -206,16 +220,20 @@ function inPlaceOf(protocol, call, note, at, placed) {
206
220
  // and a call the reply places nowhere takes the id its own stream settled
207
221
  // on for it.
208
222
  //
209
- // Both arms still invent ids in one reply, and they still name them under
210
- // different words: a counter that happened to reach another call's index
211
- // would hand two unrelated calls the same item, which is the failure the
212
- // counter was introduced to prevent.
223
+ // The id a stream already answered comes first, before the place the caller
224
+ // knows. The id the call *itself* carries comes last, and only behind the
225
+ // `msg_` the invented kind is named under: a word is not the guard's to hand
226
+ // out twice. A reply can name two items by one word (this route was measured
227
+ // settling on an id only after the frame that opened the call, and nothing
228
+ // stops the same id naming a call the reply keeps), and the note taking that
229
+ // id verbatim would key two items — the note and the item the reply kept —
230
+ // by one word, which is the failure this arm exists to prevent, read back at
231
+ // the other end. Behind the prefix the word cannot collide with an item of
232
+ // the reply, and a call_id the reply used is still named in the id it
233
+ // answers, as it was.
213
234
  const id =
214
- typeof call.id === "string" && call.id
215
- ? call.id
216
- : `msg_${
217
- call.call_id ?? placed ?? (at === undefined ? `anon_${++synthesized}` : `dropped_${at}`)
218
- }`;
235
+ placed ??
236
+ `msg_${identity(call.call_id) ?? (at === undefined ? `anon_${++synthesized}` : `dropped_${at}`)}`;
219
237
  return {
220
238
  type: "message",
221
239
  id,
@@ -237,10 +255,27 @@ function inPlaceOf(protocol, call, note, at, placed) {
237
255
  // the wire leaves with an id: the last-resort kind, process-unique under the
238
256
  // same counter the invented item ids use, so two invented calls cannot share
239
257
  // one.
258
+ //
259
+ // An invented id is only process-unique, and the counter is shared with every
260
+ // earlier reply, so it can reach a word the same reply already placed: an
261
+ // upstream that names its calls call_1, call_2, … names the very ids the
262
+ // counter mints, and a call left unnamed beside those used to be handed one of
263
+ // them — two calls of one reply under one id, which is the same failure the
264
+ // counter exists to prevent, one step further out. The ids the calls being
265
+ // flushed already carry are read first, and an invented id misses all of them.
240
266
  function ensureCallIDs(calls) {
267
+ const held = new Set();
268
+ for (const call of calls) {
269
+ const id = identity(call.id);
270
+ if (id !== undefined) held.add(id);
271
+ }
241
272
  for (const call of calls) {
242
- if (typeof call.id === "string" && call.id !== "") continue;
243
- call.id = `call_${++synthesized}`;
273
+ if (identity(call.id) !== undefined) continue;
274
+ let id;
275
+ do id = `call_${++synthesized}`;
276
+ while (held.has(id));
277
+ held.add(id);
278
+ call.id = id;
244
279
  }
245
280
  }
246
281
 
@@ -334,6 +369,11 @@ function checkSnapshots(data, protocol, checkName, placeOf) {
334
369
  return { changed, kept, left };
335
370
  }
336
371
 
372
+ // The slot a fragmented call or a choice sits in. A frame that names no index
373
+ // is read as the first one, which is what the collapse path reads it as too
374
+ // (stream.mjs's `index`): a fragment carries no other place it could hold, and
375
+ // the two paths over one stream must answer one reply (pinned by the chat test
376
+ // "a fragment that names no index is read the same by both paths").
337
377
  function position(value) {
338
378
  const result = value ?? 0;
339
379
  if (!Number.isSafeInteger(result) || result < 0)
@@ -443,7 +483,16 @@ function chatBuffer(checkName) {
443
483
  const delta = choice.delta;
444
484
  if (!object(delta)) continue;
445
485
  if (typeof delta.content === "string" && delta.content) wrote.add(position(choice.index));
446
- if (delta.tool_calls == null && delta.function_call == null) continue;
486
+ // A `tool_calls` that is there but empty is a call of nothing: a
487
+ // gateway re-emitting the shape with a role-only delta sends one, and
488
+ // no call is being waited for. Reading it as a call opened a round
489
+ // with nothing in it — the choice's `ended` mark was dropped with it,
490
+ // a choice the upstream never sent was flushed at [DONE], and a stream
491
+ // that then closed without the marker failed the whole turn "a tool
492
+ // call unfinished" while nothing was pending. It is left where it is,
493
+ // as a field the guard has nothing to say about.
494
+ const fragments = list(delta.tool_calls);
495
+ if (delta.function_call == null && !fragments.length) continue;
447
496
  changed = true;
448
497
  // A call buffered now opens a round of its own for this choice: a
449
498
  // choice that finished as a plain stop because every call of an
@@ -451,7 +500,7 @@ function chatBuffer(checkName) {
451
500
  const at = position(choice.index);
452
501
  ended.delete(at);
453
502
  const state = stateFor(at, data);
454
- for (const fragment of list(delta.tool_calls)) {
503
+ for (const fragment of fragments) {
455
504
  if (!object(fragment) || (fragment.type != null && fragment.type !== "function")) {
456
505
  throw new GuardError("Unsupported upstream tool delta");
457
506
  }
@@ -534,8 +583,33 @@ function chatBuffer(checkName) {
534
583
  // as text carrying the note, and its arguments are never sent. A message
535
584
  // whose every call was left out stops as a turn's end, not as tool use.
536
585
  function snapshotStream(protocol, checkName) {
537
- const left = new Map(); // output_index or content block index → true
538
- const leftIDs = new Set(); // Responses item ids left out
586
+ // Where a call left out was replaced, by its output_index (or a Messages
587
+ // block's index, which stores true: the id it answers is the text's, not an
588
+ // item's). A copy of the call that lands on the same place answers the id
589
+ // recorded there rather than the one the upstream names it by later.
590
+ const left = new Map();
591
+ // The places the items the reply kept hold. A place is a fact about the
592
+ // reply, so an arguments frame that gives one belongs to whoever holds it —
593
+ // the item kept there, or the call left out there — whatever words either
594
+ // was named by. Reading the place first is what makes the answer the same
595
+ // whichever order the two calls arrived in: two sets subtracted from each
596
+ // other answered by arrival order instead (a kept call named X before a
597
+ // dropped one named X let the dropped call's frames through, and the same
598
+ // reply with the two swapped did not).
599
+ const keptPlaces = new Set();
600
+ // The words the items the reply kept were named by, for the one case a place
601
+ // cannot answer: two items stating one index, one of them replaced by a note
602
+ // (an upstream at odds with itself). There the frame's own word is all that
603
+ // says whose it is, and a word a kept item was named by is that item's —
604
+ // reading the place alone would swallow the declared call's frames with the
605
+ // dropped call's.
606
+ const keptWords = new Set();
607
+ // Every word the calls this stream left out are named by: the ids the guard
608
+ // invented for them, and the ids and call_ids the upstream named them by. A
609
+ // frame that gives no place and arrives outside any item is read by its
610
+ // word, and a word a call left out was named by is never forwarded —
611
+ // reaching the client it would describe an item that was replaced by a note.
612
+ const leftIDs = new Set();
539
613
  // The calls this stream placed nowhere — its frames carried no output_index
540
614
  // and no item id — in the order it opened them. Nothing in those frames says
541
615
  // which item is which, so the order is all the guard has: the k-th item a
@@ -546,11 +620,34 @@ function snapshotStream(protocol, checkName) {
546
620
  // (inPlaceOf), which is also what its arguments frames are matched by. An
547
621
  // entry is settled once the stream has left the call.
548
622
  const nowhere = []; // { token, name, settled }
549
- // The item ids the reply has named on an item of its own: the arguments
550
- // frames of a call the reply opened carry one of these, and are not the
551
- // placeless call's to swallow.
623
+ // What each word the reply has used for a call it left out answers: the id
624
+ // the guard invented for that call, under every word the reply names it by.
625
+ // One call is replaced more than once, and the client keys an item by the id
626
+ // it was handed first — the frames of the measured route carry no id at all
627
+ // until their arguments frames name one, so the id the upstream settled on
628
+ // arrives after the replacement, and answering it then would make one call
629
+ // two items (inPlaceOf).
630
+ const minted = new Map(); // a word the call is named by → the id it answers
631
+ // The item ids the reply has named on an item of its own, and the words of
632
+ // the item the stream is inside. A frame that names one of the latter is that
633
+ // item's — the reply naming the same word elsewhere says nothing about it —
634
+ // while a frame naming one of the former belongs to that other item, which
635
+ // is what keeps an item the guard kept from being read as the one it is
636
+ // inside.
552
637
  const seen = new Set();
638
+ const insideWords = new Set();
553
639
  let open = -1; // the placeless call the stream is inside, or -1
640
+ // The item the stream is inside, as far as an arguments frame that gives no
641
+ // place of its own is concerned: the id the call left out there answers (and
642
+ // the name it was called), or undefined with `insideKept` for an item the
643
+ // reply kept. `closingID`/`closingName` are the same for the frame that
644
+ // closes an item: a copy of a call can arrive with nothing to name it by,
645
+ // and the call being closed is what it is.
646
+ let insideID;
647
+ let insideName;
648
+ let insideKept = false;
649
+ let closingID;
650
+ let closingName;
554
651
  let kept = 0;
555
652
  let dropped = 0;
556
653
  // What a frame says the reply's items are called. A frame that names none of
@@ -565,82 +662,190 @@ function snapshotStream(protocol, checkName) {
565
662
  if (object(value) && typeof value.id === "string" && value.id) seen.add(value.id);
566
663
  }
567
664
  }
568
- // Opens a placeless call of its own and answers the token naming it.
665
+ // Opens a call the reply placed nowhere — a frame with no output_index — and
666
+ // answers the entry that names it. Its id is what the guard answered for the
667
+ // call, filled in by the caller once it knows: the entry is what a later copy
668
+ // of the same call is recognised by, and that copy can carry nothing at all —
669
+ // no word of its own and no index (measured: the frame that closes the call
670
+ // drops the call_id the frame that opened it carried), so the call's name
671
+ // among the entries is the only thing left to read it by.
569
672
  function entering(name, settled = false) {
570
- const token = `anon_${++synthesized}`;
571
- nowhere.push({ token, name, settled });
673
+ const entry = { name, id: undefined, settled };
674
+ nowhere.push(entry);
572
675
  open = nowhere.length - 1;
573
- return token;
676
+ return entry;
574
677
  }
678
+ // The words a frame can name this call by: the item id and the call_id, a
679
+ // non-empty string each (identity), and both name the one call.
680
+ function names(call) {
681
+ return [identity(call.id), identity(call.call_id)].filter((value) => value !== undefined);
682
+ }
683
+ // Whether the call carries a word of its own to be placed by — the test
684
+ // inPlaceOf and ensureCallIDs make, so all three read "" the same way.
575
685
  function identified(call) {
576
- return (typeof call.id === "string" && call.id) || call.call_id != null;
686
+ return identity(call.id) !== undefined || identity(call.call_id) !== undefined;
687
+ }
688
+ // Records that this call answers `id`, under every word the reply has for it:
689
+ // the copies of the call that follow answer the same one.
690
+ function mintedFor(call, id) {
691
+ minted.set(id, id);
692
+ for (const word of names(call)) minted.set(word, id);
693
+ return id;
577
694
  }
578
- // The stream's answer for a call whose frames carry no output_index and no
579
- // item id, or undefined when the call is not one the stream has to place:
580
- // the walk hands every call it leaves out through here.
695
+ // The stream's answer for a call it leaves out: the id this call already
696
+ // answers, or undefined when the stream has none and the call keeps its own.
697
+ // The walk hands every call it leaves out through here.
581
698
  function placeOf(call, at, listed) {
582
- if (protocol !== "responses" || identified(call)) return undefined;
699
+ if (protocol !== "responses") return undefined;
700
+ // A word the reply has already used for a call it left out names that
701
+ // call: answer the id it was handed.
702
+ const bound = names(call)
703
+ .map((word) => minted.get(word))
704
+ .find((id) => id !== undefined);
705
+ if (bound !== undefined) return bound;
583
706
  if (listed) {
584
- // The copy inside a snapshot or the terminal response's output: the call
585
- // sits where the walk found it, and an entry invented when the stream
586
- // opened that call already names that place. A reply that places nothing
587
- // is what this can be read from, so the entry is matched by the place it
588
- // holds when the names agree, and by its name when they do not — the
707
+ // The copy inside a snapshot or the terminal response's output: it sits
708
+ // where the walk found it, and a call the stream already replaced at
709
+ // that place has answered an id there. A reply that places nothing is
710
+ // what the rest can be read from, so the entry is matched by the place
711
+ // it holds when the names agree, and by its name when they do not — the
589
712
  // same call is copied into several snapshots, and every one of them has
590
713
  // to answer the same id. Two calls of one name cannot be told apart
591
- // here, and the last one the stream opened answers for both.
714
+ // here, and the last one the stream opened answers for both, and a call
715
+ // that carries a word of its own is not guessed at from a name.
716
+ const held = left.get(at);
717
+ if (typeof held === "string") return held;
718
+ if (identified(call)) return undefined;
592
719
  const entry =
593
720
  (nowhere[at]?.name === call.name ? nowhere[at] : undefined) ??
594
721
  nowhere.findLast((value) => value.name === call.name);
595
- return entry?.token;
722
+ return entry?.id;
723
+ }
724
+ // A frame's own item. One that gave an index is placed by it: the call the
725
+ // stream already replaced at that place answered an id there, and the
726
+ // dropped_<at> token inPlaceOf mints is what a call the stream never
727
+ // replaced gets.
728
+ if (at !== undefined) {
729
+ const held = left.get(at);
730
+ return typeof held === "string" ? held : undefined;
731
+ }
732
+ const inside = nowhere[open] && !nowhere[open].settled ? nowhere[open] : undefined;
733
+ if (identified(call)) {
734
+ // The frame that closes a call the stream opened with nothing to place it
735
+ // by can be the first of its frames to carry an id (measured). It is the
736
+ // call the stream is inside, and the id already answered is what the
737
+ // client has.
738
+ if (!inside) return undefined;
739
+ inside.settled = true;
740
+ return mintedFor(call, inside.id);
596
741
  }
597
- // A frame's own item. One that gave an index is already placed by it (the
598
- // dropped_<at> token inPlaceOf mints), so only the index-less frames need
599
- // the stream's answer: the call of that name it is inside, or else the
600
- // call it opened last — which is what a frame that repeats no name is.
601
- if (at !== undefined) return undefined;
602
- const entry =
603
- nowhere.findLast((value) => value.name === call.name) ??
604
- (nowhere[open] && !nowhere[open].settled ? nowhere[open] : undefined);
742
+ // A frame with nothing at all to name it by: the call the stream opened
743
+ // last of that name, which is the one it is closing — the id it answered
744
+ // is the one the client was handed, and a later copy of the call has to
745
+ // answer it again rather than mint one of its own.
746
+ const entry = nowhere.findLast((value) => value.name === call.name) ?? inside;
605
747
  if (entry) {
606
748
  entry.settled = true;
607
- return entry.token;
749
+ return mintedFor(call, entry.id);
608
750
  }
751
+ // The call the stream is closing, in a frame that carries nothing to name
752
+ // it by: the id the guard answered for it is what this copy answers, so
753
+ // the call's rewrites stay one item. A frame of another call that only
754
+ // looks like it — same name, no words — is read the same way, and that is
755
+ // the closest thing to a word either of them has.
756
+ if (closingID !== undefined && closingName === call.name) return mintedFor(call, closingID);
609
757
  // A done frame whose added frame was never seen. It is a call of its own,
610
758
  // and the terminal's copy of it still answers this id by name.
611
- return entering(call.name, true);
759
+ const opened = entering(call.name, true);
760
+ opened.id = `msg_anon_${++synthesized}`;
761
+ return mintedFor(call, opened.id);
612
762
  }
613
763
  return {
614
764
  apply(data, type) {
615
765
  if (protocol === "responses") {
616
766
  see(data);
617
- // A frame that opens an item ends the window the last one had: the
618
- // stream is not inside that call any more, so an arguments frame
619
- // naming an item the reply never opened is not that call's to swallow
620
- // (below). The item this frame opens is entered just after this, in
621
- // the frame's own branch.
622
- if (type === "response.output_item.added" && nowhere[open]) nowhere[open].settled = true;
623
767
  const item = data.item;
624
- if (
625
- type === "response.output_item.added" &&
768
+ const opens = type === "response.output_item.added";
769
+ const closes = type === "response.output_item.done";
770
+ // A frame that opens or closes an item ends the window the last one
771
+ // had: the stream is not inside that item any more, so an arguments
772
+ // frame that gives no place of its own is not that item's to answer
773
+ // for (below). The item this frame opens is entered just after this,
774
+ // in the frame's own branch.
775
+ if (opens || closes) {
776
+ // The item the stream was inside, for the frame that closes it: a
777
+ // copy of a call can arrive with nothing at all to name it by — no
778
+ // word of its own and no index — and the call being closed is the
779
+ // one whose id it answers.
780
+ closingID = closes ? insideID : undefined;
781
+ closingName = closes ? insideName : undefined;
782
+ insideID = undefined;
783
+ insideName = undefined;
784
+ insideKept = false;
785
+ insideWords.clear();
786
+ if (opens && nowhere[open]) nowhere[open].settled = true;
787
+ }
788
+ // The item this frame carries, when it is a call of no tool the agent
789
+ // has: the walk below replaces it — opens or closes it, the copy is
790
+ // the same call — and its words are the ones a note takes the place
791
+ // of. Every other item the frame names is one the reply keeps, and the
792
+ // place it holds is a fact the suppression below reads before any word.
793
+ const leaving =
794
+ (opens || closes) &&
626
795
  object(item) &&
627
796
  item.type === "function_call" &&
628
- checkName(item.name) == null
629
- ) {
797
+ checkName(item.name) == null;
798
+ // A copy of a call left out — the frame that closes it, and every copy
799
+ // of it a snapshot or the terminal carries — holds no place of its own:
800
+ // the place the call holds is the one the frame that opened it was
801
+ // answered at, and a reply at odds with itself (one copy numbered and
802
+ // the next not, or two copies numbered differently) reads back as one
803
+ // item only when the copies leave the placing to the stream's own
804
+ // answer. The id they all answer is what the collapse path finds them
805
+ // by. The frame that opens the call keeps its index: that is where the
806
+ // place comes from.
807
+ if (closes && leaving && data.output_index !== undefined) delete data.output_index;
808
+ if ((opens || closes) && object(item) && !leaving) {
809
+ if (data.output_index !== undefined) keptPlaces.add(data.output_index);
810
+ for (const id of names(item)) keptWords.add(id);
811
+ // A function_call the reply kept: an arguments frame that gives no
812
+ // place of its own is this call's while the stream is inside it.
813
+ if (opens && item.type === "function_call") {
814
+ insideKept = true;
815
+ for (const id of names(item)) insideWords.add(id);
816
+ }
817
+ }
818
+ if (opens && leaving) {
630
819
  const note = droppedNote(item.name);
631
820
  // The frame's output_index is this call's place in the reply, which
632
821
  // is the id its later frames (done, the terminal's output) invent
633
822
  // for it again (inPlaceOf). A frame that gives no index leaves the
634
823
  // place to the stream: the entry opened here is what its later
635
- // frames — and its arguments frames — are matched by.
636
- const placed =
637
- data.output_index === undefined && !identified(item)
638
- ? entering(item.name)
639
- : undefined;
640
- const message = inPlaceOf(protocol, item, note, data.output_index, placed);
641
- left.set(data.output_index, true);
642
- leftIDs.add(message.id);
643
- if (typeof item.id === "string") leftIDs.add(item.id);
824
+ // frames — and its arguments frames — are matched by, and it is
825
+ // opened whether or not the frame carried a word, because the frame
826
+ // that closes the call can carry none (measured: the call_id the
827
+ // added frame named is gone from the done frame).
828
+ const entry = data.output_index === undefined ? entering(item.name) : undefined;
829
+ const message = inPlaceOf(protocol, item, note, data.output_index, entry?.id);
830
+ if (entry) entry.id = message.id;
831
+ // Every word this call can be named by later is recorded, whichever
832
+ // shape its frames have: the id the guard answered for it (with the
833
+ // place in it), and the id and call_id the upstream gave it — the
834
+ // arguments frames of a call that gave an index name it by one of
835
+ // those alone, so a set of invented ids would let them through,
836
+ // describing an item the client is told was replaced by a note. The
837
+ // place it answered at is recorded too, so the copies of it that
838
+ // follow answer the same id rather than the one the upstream settles
839
+ // on after them.
840
+ if (data.output_index !== undefined) left.set(data.output_index, message.id);
841
+ for (const id of [message.id, ...names(item)]) leftIDs.add(id);
842
+ mintedFor(item, message.id);
843
+ // And the stream is inside it: its arguments frames carry no place
844
+ // of their own, and this window is what says they are its frames
845
+ // rather than an item the reply never opened.
846
+ insideID = message.id;
847
+ insideName = item.name;
848
+ for (const id of [message.id, ...names(item)]) insideWords.add(id);
644
849
  dropped++;
645
850
  const where = { item_id: message.id, output_index: data.output_index, content_index: 0 };
646
851
  const part = { type: "output_text", text: "", annotations: [] };
@@ -656,27 +861,40 @@ function snapshotStream(protocol, checkName) {
656
861
  };
657
862
  }
658
863
  if (typeof type === "string" && type.startsWith("response.function_call_arguments.")) {
659
- if (data.output_index != null && left.has(data.output_index))
660
- return { changed: true, data: null };
661
- if (leftIDs.has(data.item_id)) return { changed: true, data: null };
662
- // An item id the reply never named on an item of its own: the
663
- // arguments frames of a call it placed nowhere name the item from
664
- // the first of them on (measured: the added frame carries no id at
665
- // all, and the frames after it carry only an item_id), so while the
666
- // stream is inside that call they are its frames. They are left out
667
- // with the call — reaching the client they would describe an item
668
- // that was replaced by a note, and the collapse path refuses them —
669
- // and the id is recorded, so the frames after the first are matched
670
- // too. A frame naming an item the reply did name belongs to that
671
- // item and is left alone.
672
- const entry = nowhere[open];
673
- if (
674
- data.output_index == null &&
675
- entry &&
676
- !entry.settled &&
677
- !seen.has(data.item_id)
678
- ) {
679
- if (typeof data.item_id === "string" && data.item_id) leftIDs.add(data.item_id);
864
+ // Whose frame this is, by what the frame itself says: the place it
865
+ // gives is a fact about the reply and is read first, whoever holds
866
+ // it; a frame that gives none belongs to the item the stream is
867
+ // inside; and one that arrives outside any item is read by its word.
868
+ // The answer is the same whichever order the calls arrived in,
869
+ // because no step of it subtracts one set of words from another.
870
+ const place = data.output_index;
871
+ const word = identity(data.item_id);
872
+ let holder = "kept";
873
+ if (place != null && left.has(place) && !keptPlaces.has(place)) holder = "dropped";
874
+ // Two items stated one index: the place cannot say whose the frame
875
+ // is, and the word it names is what is left.
876
+ else if (place != null && word !== undefined && keptWords.has(word)) holder = "kept";
877
+ else if (place != null && left.has(place)) holder = "dropped";
878
+ else if (place != null && keptPlaces.has(place)) holder = "kept";
879
+ else if (place == null && insideID !== undefined)
880
+ // A word the reply named on some other item is that item's frame,
881
+ // interleaved with this call's own: the call the stream is inside
882
+ // answers for the words it was named by, and for a word the reply
883
+ // names nowhere else (measured: the added frame carries no id, and
884
+ // the frames after it name one).
885
+ holder =
886
+ word !== undefined && seen.has(word) && !insideWords.has(word) ? "kept" : "dropped";
887
+ else if (place == null && insideKept) holder = "kept";
888
+ else if (word !== undefined && leftIDs.has(word)) holder = "dropped";
889
+ if (holder === "dropped") {
890
+ if (word !== undefined) {
891
+ leftIDs.add(word);
892
+ // The id names the call the stream is inside, so a copy of that
893
+ // call which later carries it answers the id the client was
894
+ // handed (placeOf).
895
+ const answers = (place != null ? left.get(place) : undefined) ?? insideID;
896
+ if (answers !== undefined) minted.set(word, answers);
897
+ }
680
898
  return { changed: true, data: null };
681
899
  }
682
900
  }
@@ -736,7 +954,10 @@ async function readBounded(response) {
736
954
  if (!reader) return "";
737
955
  const parts = [];
738
956
  let bytes = 0;
739
- let failure;
957
+ // Named for the read it ends, not `failure`: that word is this module's
958
+ // error constructor, and a local of the same name reads as if the guard's
959
+ // own failures were being built here.
960
+ let readFailure;
740
961
  try {
741
962
  while (true) {
742
963
  const { done, value } = await reader.read();
@@ -747,7 +968,7 @@ async function readBounded(response) {
747
968
  parts.push(value);
748
969
  }
749
970
  } catch (error) {
750
- failure = error;
971
+ readFailure = error;
751
972
  }
752
973
  // Whatever ended the read — the limit, a malformed body, or the end of it —
753
974
  // this body is never read again, so the stream is cancelled and the reader
@@ -762,12 +983,12 @@ async function readBounded(response) {
762
983
  try {
763
984
  await reader.cancel();
764
985
  } catch (error) {
765
- if (!failure) throw error;
766
- if (failure instanceof Error && !("cause" in failure)) failure.cause = error;
986
+ if (!readFailure) throw error;
987
+ if (readFailure instanceof Error && !("cause" in readFailure)) readFailure.cause = error;
767
988
  } finally {
768
989
  reader.releaseLock();
769
990
  }
770
- if (failure) throw failure;
991
+ if (readFailure) throw readFailure;
771
992
  const all = new Uint8Array(bytes);
772
993
  let offset = 0;
773
994
  for (const part of parts) {
@@ -1125,7 +1346,12 @@ function guardedStream(response, protocol, checkName, endCutShort) {
1125
1346
  )
1126
1347
  completed = true;
1127
1348
  if (result || extra.length) {
1128
- const own = !result ? "" : changed ? rewrite(frame, result) : frame.raw;
1349
+ // A frame the walk left as it came is forwarded as it came: only
1350
+ // its own bytes carry what a rewrite would drop — the event name
1351
+ // it was sent under, and any field of the frame the guard does
1352
+ // not read.
1353
+ let own = "";
1354
+ if (result) own = changed ? rewrite(frame, result) : frame.raw;
1129
1355
  const added = extra.map((value) => encodeEvent(value, value.type)).join("");
1130
1356
  controller.enqueue(encoder.encode(own + added));
1131
1357
  return;