Skip to content

Commit fca7462

Browse files
authored
Preserve in-flight executions after unregistration (#248)
* Preserve in-flight executions after unregistration * Simplify parallel nesting
1 parent 83e893a commit fca7462

1 file changed

Lines changed: 4 additions & 49 deletions

File tree

‎index.bs‎

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ To <dfn>cancel a pending tool execution</dfn> given a [=traversable navigable=]
249249
1. If |traversable|'s [=traversable navigable/pending tool executions map=][|uuid|] does not
250250
[=map/exist=], then return.
251251

252-
Note: See <a href=#unregistration-race>this note</a> to learn how a tool's natural
252+
Note: See <a href=#pending-execution-removal-race>this note</a> to learn how a tool's natural
253253
resolution/rejection can race with the caller's cancellation. This might result in the pending
254254
execution entry for |uuid| being removed before we get here. In that case, the
255255
{{ModelContext/executeTool()}} promise will still be rejected with the abort
@@ -556,30 +556,8 @@ To <dfn for="model context">unregister a tool</dfn> given a {{ModelContext}} |mo
556556
1. Let |targetDocument| be |modelContext|'s [=relevant global object=]'s [=associated
557557
Document|associated <code>Document</code>=].
558558

559-
1. Let |traversable| be |targetDocument|'s [=node navigable=]'s [=navigable/traversable navigable=].
560-
561-
1. Run the following steps [=in parallel=]:
562-
563-
1. Let |canceledExecutions| be an empty [=list=].
564-
565-
1. [=map/For each=] |uuid| &rarr; |execution| of |traversable|'s [=traversable navigable/pending
566-
tool executions map=]:
567-
568-
1. If |execution|'s [=pending tool execution/target document=] is |targetDocument| and
569-
|execution|'s [=pending tool execution/tool name=] is |tool name|, then [=list/append=]
570-
|uuid| to |canceledExecutions|.
571-
572-
1. [=list/For each=] |uuid| of |canceledExecutions|:
573-
574-
1. Let |execution| be |traversable|'s [=traversable navigable/pending tool executions
575-
map=][|uuid|].
576-
577-
1. Run |execution|'s [=pending tool execution/completion steps=] given null and false.
578-
579-
Note: This removes |execution| from the [=traversable navigable/pending tool executions
580-
map=].
581-
582-
1. [=Notify documents of a tool change=] given |targetDocument| and |exposed origins|.
559+
1. [=In parallel=], [=notify documents of a tool change=] given |targetDocument| and |exposed
560+
origins|.
583561

584562
</div>
585563

@@ -1021,37 +999,14 @@ The <dfn method for=ModelContext>executeTool(<var>tool</var>, <var>inputObject</
1021999
[=traversable navigable/pending tool executions map=][|uuid|] does not [=map/exist=], then
10221000
return.
10231001

1024-
<div class=note>
1002+
<div class=note id=pending-execution-removal-race>
10251003
<p>It is possible that a pending execution identified by |uuid| no longer exists. This can
10261004
happen due to a race between (a) tool cancellation when the caller document <a
10271005
href=#caller-destroyed-cleanup>gets destroyed</a> or when the caller aborts the
10281006
execution via the options signal; and (b) tool promise resolution. Both
10291007
of these race to invoke |completionSteps|, and the first invocation will remove the
10301008
pending execution by its key |uuid|, this check protects subsequent racing
10311009
invocations.</p>
1032-
1033-
<p>This can also happen due to a limitation with tool unregistration. Consider this
1034-
example:</p>
1035-
1036-
<div class=example id=unregistration-race>
1037-
<xmp class=language-js>
1038-
const ac = new AbortController();
1039-
document.modelContext.registerTool({
1040-
name: "tool"
1041-
description: "a racy tool",
1042-
execute: async () => {
1043-
return new Promise(resolve => {
1044-
resolve("tool result");
1045-
ac.abort();
1046-
});
1047-
}
1048-
}, {signal: ac.signal});
1049-
</xmp>
1050-
1051-
<p>In this case, tool unregistration happens after a tool Promise is resolved, but before
1052-
its fulfillment handler runs. Since both run |completionSteps|, this check protects the
1053-
second invocation from trying to remove the same pending execution twice.</p>
1054-
</div>
10551010
</div>
10561011

10571012
1. [=map/Remove=] |targetDocument|'s [=node navigable=]'s [=navigable/traversable

0 commit comments

Comments
 (0)