@loomwork/dispatcher 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +83 -2
- package/dist/LocalDispatcher.d.ts +56 -0
- package/dist/LocalDispatcher.d.ts.map +1 -0
- package/dist/LocalDispatcher.js +104 -0
- package/dist/LocalDispatcher.js.map +1 -0
- package/dist/PlacementDispatcher.d.ts +45 -0
- package/dist/PlacementDispatcher.d.ts.map +1 -0
- package/dist/PlacementDispatcher.js +95 -0
- package/dist/PlacementDispatcher.js.map +1 -0
- package/dist/SupervisingDispatcher.d.ts +57 -0
- package/dist/SupervisingDispatcher.d.ts.map +1 -0
- package/dist/SupervisingDispatcher.js +119 -0
- package/dist/SupervisingDispatcher.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/placement.d.ts +5 -0
- package/dist/placement.d.ts.map +1 -0
- package/dist/placement.js +28 -0
- package/dist/placement.js.map +1 -0
- package/dist/types.d.ts +69 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +21 -0
- package/dist/types.js.map +1 -0
- package/package.json +53 -3
- package/src/LocalDispatcher.ts +141 -0
- package/src/PlacementDispatcher.ts +124 -0
- package/src/SupervisingDispatcher.ts +160 -0
- package/src/index.ts +27 -0
- package/src/placement.ts +26 -0
- package/src/types.ts +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @loomwork/dispatcher — placement
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The dispatcher decides where an execution runs and owns the allocate, execute, release cycle around a
|
|
4
|
+
[`WorkerRuntime`](../worker-runtime/README.md): worker allocation, retries, a per-attempt timeout, and
|
|
5
|
+
result collection. It sits between whatever starts executions (the A2A bus, `executeGraph`, timers) and a
|
|
6
|
+
worker, so moving an execution off-box is a change of worker, not of caller. It ships three composable
|
|
7
|
+
dispatchers. Most apps do not touch this package: the default is in-process. You use it to place work on
|
|
8
|
+
named targets (local, HTTP, Cloud Run), to add restart and circuit-breaking, or to write your own
|
|
9
|
+
dispatcher.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import {
|
|
13
|
+
LocalDispatcher,
|
|
14
|
+
PlacementDispatcher,
|
|
15
|
+
SupervisingDispatcher,
|
|
16
|
+
defaultPlacementPolicy,
|
|
17
|
+
} from "@loomwork/dispatcher";
|
|
18
|
+
import { LocalWorkerRuntime } from "@loomwork/worker-runtime-local";
|
|
19
|
+
import { app } from "./config"; // your defineConfig({ storage, flows, ... }) result
|
|
20
|
+
|
|
21
|
+
const local = new LocalDispatcher(new LocalWorkerRuntime(app.runtime), { maxAttempts: 2 });
|
|
22
|
+
|
|
23
|
+
// Named targets; `meta.target` picks one, anything else falls to `default`.
|
|
24
|
+
const placement = new PlacementDispatcher({
|
|
25
|
+
targets: { local, sized: local /* e.g. a LocalDispatcher over a RemoteWorkerRuntime */ },
|
|
26
|
+
default: "local",
|
|
27
|
+
policy: defaultPlacementPolicy("local"),
|
|
28
|
+
onWarn: (message) => console.warn(message),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Restart a throwing dispatch twice with backoff, and open a circuit after 5 straight failures.
|
|
32
|
+
const dispatcher = new SupervisingDispatcher(placement, {
|
|
33
|
+
policy: { maxRestarts: 2, backoffMs: 200, circuit: { threshold: 5, cooldownMs: 30_000 } },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const result = await dispatcher.dispatch(
|
|
37
|
+
{ executionId: "hello-2", workflowId: "hello", flowId: "hello", input: { name: "Grace" } },
|
|
38
|
+
{ target: "sized", priority: 1 }
|
|
39
|
+
);
|
|
40
|
+
console.log(result.status, await dispatcher.health());
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Surface
|
|
44
|
+
|
|
45
|
+
- **Contract.** `ExecutionDispatcher` (`dispatch`, `cancel`, `health`), `DispatchMetadata` (`lane`,
|
|
46
|
+
`priority`, `estimatedCpu`, `estimatedMemory`, `estimatedDuration`, `target`), `DispatcherHealth`,
|
|
47
|
+
`PlacementPolicy`, and `DISPATCHER_SPI_VERSION` (1.2.0). `ExecutionPackage`, `ExecutionResult` and
|
|
48
|
+
`ExecutionOutcome` are re-exported from `@loomwork/worker-runtime`.
|
|
49
|
+
- **`LocalDispatcher(worker, { maxAttempts, executeTimeoutMs })`.** One worker: allocate, execute, release.
|
|
50
|
+
- **`PlacementDispatcher({ targets, default, policy?, onWarn? })`.** Routes each dispatch to a named
|
|
51
|
+
target dispatcher. `defaultPlacementPolicy` honours `meta.target` when it is registered;
|
|
52
|
+
`dispatchMetadataFromPolicy` maps a flow's `ExecutionPolicy` (compute target, priority, resources,
|
|
53
|
+
affinity) onto `DispatchMetadata`.
|
|
54
|
+
- **`SupervisingDispatcher(child, { policy })`.** Restart with backoff (`maxRestarts`, `backoffMs`,
|
|
55
|
+
`backoffFactor`) and a circuit breaker (`CircuitPolicy`), failing fast with `CircuitOpenError`.
|
|
56
|
+
- **Wiring.** `defineConfig({ execution: { dispatcher } })` supplies the placement dispatcher for the A2A
|
|
57
|
+
bus; the default is in-process.
|
|
58
|
+
|
|
59
|
+
## Behaviour that surprises people
|
|
60
|
+
|
|
61
|
+
- **A failed flow is not a failed dispatch.** `maxAttempts` and `maxRestarts` apply when the worker or
|
|
62
|
+
transport throws or an attempt times out, not when the flow returns `status: "failed"`. `maxAttempts`
|
|
63
|
+
defaults to 1 and `maxRestarts` to 0.
|
|
64
|
+
- **A superseded refusal is never retried.** If another holder owns the execution, the dispatcher
|
|
65
|
+
rethrows at once, `SupervisingDispatcher` neither restarts nor counts it against the circuit, and
|
|
66
|
+
`executeGraph` rejects its join on it. Await that execution by id instead.
|
|
67
|
+
- **An unknown target falls back to `default`, with a warning.** It is warned once per name (the default
|
|
68
|
+
sink is `console.warn`), because a placement hint can outlive the target it names.
|
|
69
|
+
- **The circuit key defaults to the flow id**, so the breaker is per flow unless you pass `key`.
|
|
70
|
+
- **`cancel` goes to the target the execution was routed to**, or to every target when that is no longer
|
|
71
|
+
remembered; a stray cancel on a finished execution is ignored.
|
|
72
|
+
- **`executeTimeoutMs` abandons the wait, not necessarily the worker.** A hung instance may keep running,
|
|
73
|
+
which is why redispatch relies on fencing in the store.
|
|
74
|
+
|
|
75
|
+
## Install
|
|
76
|
+
|
|
77
|
+
Unpublished for now: inside this repo use `workspace:*`; from a sibling checkout use a `link:` override (see [AGENTS.md](../../AGENTS.md)).
|
|
78
|
+
|
|
79
|
+
## See also
|
|
80
|
+
|
|
81
|
+
- API reference: section "`@loomwork/dispatcher` — placement" in [docs/api-reference.org](../../docs/api-reference.org), which still quotes `DISPATCHER_SPI_VERSION` as 1.1.0.
|
|
82
|
+
- Guides: [user guide, section 13: where executions run](../../docs/user-guide.md#13-where-executions-run-compute), [scaling](../../docs/scaling.md).
|
|
83
|
+
- ADRs: [0001 dispatcher and worker runtime](../../docs/adr/0001-execution-dispatcher-and-worker-runtime.md), [0004 failure recovery](../../docs/adr/0004-failure-recovery.md), [0033 SPI conformance](../../docs/adr/0033-spi-conformance-contracts.md).
|
|
84
|
+
- Source: `packages/dispatcher/src`; `runExecutionDispatcherConformance` in `@loomwork/conformance` proves a dispatcher.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type ExecutionPackage, type ExecutionResult, type WorkerRuntime } from "@loomwork/worker-runtime";
|
|
2
|
+
import type { DispatchMetadata, DispatcherHealth, ExecutionDispatcher } from "./types";
|
|
3
|
+
export interface LocalDispatcherOptions {
|
|
4
|
+
/**
|
|
5
|
+
* How many times to attempt a dispatch before giving up. An attempt fails when
|
|
6
|
+
* the worker throws (transport/allocation error or a crashed instance) or when
|
|
7
|
+
* it exceeds {@link LocalDispatcherOptions.executeTimeoutMs} — NOT when the flow
|
|
8
|
+
* returns a "failed" result, which is a normal terminal outcome. Default 1.
|
|
9
|
+
*
|
|
10
|
+
* NOR when the worker was refused because another holder owns the execution — a lost
|
|
11
|
+
* start race or a fenced-out attempt ({@link isSupersededRefusal}, L20). That refusal is
|
|
12
|
+
* rethrown at once, never retried: `allocate` fences every attempt, so a retry would
|
|
13
|
+
* supersede the holder that won, and the two would ping-pong, re-running the in-flight
|
|
14
|
+
* step on every bounce. Declining it keeps ADR 0004's bound — the one interrupted step
|
|
15
|
+
* runs at most twice — whatever this is set to.
|
|
16
|
+
*
|
|
17
|
+
* Redispatch is safe: the worker is idempotent (it inspects durable state and
|
|
18
|
+
* resumes a crashed execution from its latest snapshot rather than restarting),
|
|
19
|
+
* so a retry continues where the failed attempt left off without re-running
|
|
20
|
+
* completed steps.
|
|
21
|
+
*/
|
|
22
|
+
maxAttempts?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Per-attempt deadline. A worker that hangs (a wedged/lost Cloud Run instance)
|
|
25
|
+
* is treated as a failed attempt and redispatched. Omit to wait indefinitely.
|
|
26
|
+
*/
|
|
27
|
+
executeTimeoutMs?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The default Dispatcher. It wraps a single {@link WorkerRuntime} — allocate a
|
|
31
|
+
* lease, execute the package, release the lease — and exposes the placement seam
|
|
32
|
+
* without any cloud awareness. Swapping the worker (Local → Remote → CloudRun)
|
|
33
|
+
* is the only change needed to move execution off-box; callers see the same
|
|
34
|
+
* {@link ExecutionResult} contract.
|
|
35
|
+
*/
|
|
36
|
+
export declare class LocalDispatcher implements ExecutionDispatcher {
|
|
37
|
+
private readonly worker;
|
|
38
|
+
private inFlight;
|
|
39
|
+
private readonly maxAttempts;
|
|
40
|
+
private readonly executeTimeoutMs;
|
|
41
|
+
private readonly leases;
|
|
42
|
+
private readonly cancelled;
|
|
43
|
+
constructor(worker: WorkerRuntime, options?: LocalDispatcherOptions);
|
|
44
|
+
dispatch(pkg: ExecutionPackage, meta?: DispatchMetadata): Promise<ExecutionResult>;
|
|
45
|
+
/**
|
|
46
|
+
* Best-effort cancellation. Releases any lease held for the execution, and
|
|
47
|
+
* (K8.5) forwards the kill to the worker when it supports mid-flight
|
|
48
|
+
* interruption — in-process this aborts the in-flight effect and records the
|
|
49
|
+
* durable EXECUTION_CANCELLED; over HTTP it reaches the remote worker's
|
|
50
|
+
* `POST /cancel`. Delivery stays best-effort: durable settlement belongs to
|
|
51
|
+
* the store-side cancel (fence-then-cancel), never to this call landing.
|
|
52
|
+
*/
|
|
53
|
+
cancel(executionId: string): Promise<void>;
|
|
54
|
+
health(): Promise<DispatcherHealth>;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=LocalDispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalDispatcher.d.ts","sourceRoot":"","sources":["../src/LocalDispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,eAAe,EAEpB,KAAK,aAAa,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEvF,MAAM,WAAW,sBAAsB;IACrC;;;;;;;;;;;;;;;;;OAiBG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAID;;;;;;GAMG;AACH,qBAAa,eAAgB,YAAW,mBAAmB;IAQvD,OAAO,CAAC,QAAQ,CAAC,MAAM;IAPzB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAqB;IACtD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkC;IACzD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;gBAG5B,MAAM,EAAE,aAAa,EACtC,OAAO,GAAE,sBAA2B;IAMhC,QAAQ,CAAC,GAAG,EAAE,gBAAgB,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC;IAoCxF;;;;;;;OAOG;IACG,MAAM,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAc1C,MAAM,IAAI,OAAO,CAAC,gBAAgB,CAAC;CAG1C"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalDispatcher = void 0;
|
|
4
|
+
const worker_runtime_1 = require("@loomwork/worker-runtime");
|
|
5
|
+
class DispatchTimeoutError extends Error {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The default Dispatcher. It wraps a single {@link WorkerRuntime} — allocate a
|
|
9
|
+
* lease, execute the package, release the lease — and exposes the placement seam
|
|
10
|
+
* without any cloud awareness. Swapping the worker (Local → Remote → CloudRun)
|
|
11
|
+
* is the only change needed to move execution off-box; callers see the same
|
|
12
|
+
* {@link ExecutionResult} contract.
|
|
13
|
+
*/
|
|
14
|
+
class LocalDispatcher {
|
|
15
|
+
worker;
|
|
16
|
+
inFlight = 0;
|
|
17
|
+
maxAttempts;
|
|
18
|
+
executeTimeoutMs;
|
|
19
|
+
leases = new Map();
|
|
20
|
+
cancelled = new Set();
|
|
21
|
+
constructor(worker, options = {}) {
|
|
22
|
+
this.worker = worker;
|
|
23
|
+
this.maxAttempts = Math.max(1, options.maxAttempts ?? 1);
|
|
24
|
+
this.executeTimeoutMs = options.executeTimeoutMs;
|
|
25
|
+
}
|
|
26
|
+
async dispatch(pkg, meta) {
|
|
27
|
+
this.cancelled.delete(pkg.executionId);
|
|
28
|
+
this.inFlight++;
|
|
29
|
+
try {
|
|
30
|
+
let lastErr;
|
|
31
|
+
for (let attempt = 1; attempt <= this.maxAttempts; attempt++) {
|
|
32
|
+
const lease = await this.worker.allocate({
|
|
33
|
+
executionId: pkg.executionId,
|
|
34
|
+
...(meta !== undefined && { metadata: meta }),
|
|
35
|
+
...(pkg.principal !== undefined && { principal: pkg.principal }),
|
|
36
|
+
});
|
|
37
|
+
this.leases.set(pkg.executionId, lease);
|
|
38
|
+
try {
|
|
39
|
+
const run = this.worker.execute(lease, pkg);
|
|
40
|
+
return await (this.executeTimeoutMs
|
|
41
|
+
? withTimeout(run, this.executeTimeoutMs, pkg.executionId)
|
|
42
|
+
: run);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
// L20 — superseded, not failed: someone else is driving this execution now.
|
|
46
|
+
if ((0, worker_runtime_1.isSupersededRefusal)(err, pkg.executionId))
|
|
47
|
+
throw err;
|
|
48
|
+
lastErr = err;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
this.leases.delete(pkg.executionId);
|
|
52
|
+
await this.worker.release(lease);
|
|
53
|
+
}
|
|
54
|
+
// A cancel during this attempt aborts it; DON'T redispatch a cancelled execution
|
|
55
|
+
// (that would re-run what the caller just cancelled). Also stops the flag leaking.
|
|
56
|
+
if (this.cancelled.has(pkg.executionId))
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
throw lastErr;
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
this.inFlight--;
|
|
63
|
+
this.cancelled.delete(pkg.executionId);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Best-effort cancellation. Releases any lease held for the execution, and
|
|
68
|
+
* (K8.5) forwards the kill to the worker when it supports mid-flight
|
|
69
|
+
* interruption — in-process this aborts the in-flight effect and records the
|
|
70
|
+
* durable EXECUTION_CANCELLED; over HTTP it reaches the remote worker's
|
|
71
|
+
* `POST /cancel`. Delivery stays best-effort: durable settlement belongs to
|
|
72
|
+
* the store-side cancel (fence-then-cancel), never to this call landing.
|
|
73
|
+
*/
|
|
74
|
+
async cancel(executionId) {
|
|
75
|
+
const lease = this.leases.get(executionId);
|
|
76
|
+
if (lease) {
|
|
77
|
+
// Only flag as cancelled when a dispatch is actually in flight (a lease is held) —
|
|
78
|
+
// the flag exists solely to stop that dispatch's retry loop, and gating it here keeps
|
|
79
|
+
// `cancelled` bounded (it's cleared in dispatch's finally). A cancel with no in-flight
|
|
80
|
+
// dispatch has nothing to short-circuit.
|
|
81
|
+
this.cancelled.add(executionId);
|
|
82
|
+
this.leases.delete(executionId);
|
|
83
|
+
await this.worker.release(lease);
|
|
84
|
+
}
|
|
85
|
+
await this.worker.cancel?.(executionId);
|
|
86
|
+
}
|
|
87
|
+
async health() {
|
|
88
|
+
return { healthy: true, inFlight: this.inFlight };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.LocalDispatcher = LocalDispatcher;
|
|
92
|
+
function withTimeout(p, ms, executionId) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const timer = setTimeout(() => reject(new DispatchTimeoutError(`dispatch timed out after ${ms}ms for ${executionId}`)), ms);
|
|
95
|
+
p.then((v) => {
|
|
96
|
+
clearTimeout(timer);
|
|
97
|
+
resolve(v);
|
|
98
|
+
}, (err) => {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
reject(err);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=LocalDispatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalDispatcher.js","sourceRoot":"","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G,CAAC,WAAW,CAAC,CAAC;YAChC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;YAChC,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC,CAAC;IAC1C,CAAC;IAED,KAAK,CAAC,MAAM;QACV,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;IACpD,CAAC;CACF;AA5ED,0CA4EC;AAED,SAAS,WAAW,CAAI,CAAa,EAAE,EAAU,EAAE,WAAmB;IACpE,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACxC,MAAM,KAAK,GAAG,UAAU,CACtB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,oBAAoB,CAAC,4BAA4B,EAAE,UAAU,WAAW,EAAE,CAAC,CAAC,EAC7F,EAAE,CACH,CAAC;QACF,CAAC,CAAC,IAAI,CACJ,CAAC,CAAC,EAAE,EAAE;YACJ,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,OAAO,CAAC,CAAC,CAAC,CAAC;QACb,CAAC,EACD,CAAC,GAAY,EAAE,EAAE;YACf,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,CAAC,GAAY,CAAC,CAAC;QACvB,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ExecutionPackage, ExecutionResult } from "@loomwork/worker-runtime";
|
|
2
|
+
import type { DispatchMetadata, DispatcherHealth, ExecutionDispatcher, PlacementPolicy } from "./types";
|
|
3
|
+
export interface PlacementDispatcherOptions {
|
|
4
|
+
/** Named compute targets. Each is a full ExecutionDispatcher (e.g. a LocalDispatcher
|
|
5
|
+
* wrapping a local / remote-HTTP / Cloud Run WorkerRuntime), so retry/timeout/lease
|
|
6
|
+
* handling is reused per target. */
|
|
7
|
+
targets: Record<string, ExecutionDispatcher>;
|
|
8
|
+
/** Target used when the policy returns an unregistered name. Must be a key of `targets`. */
|
|
9
|
+
default: string;
|
|
10
|
+
/** How to choose a target. Defaults to "honour meta.target, else default". */
|
|
11
|
+
policy?: PlacementPolicy;
|
|
12
|
+
/**
|
|
13
|
+
* Where to report a dispatch that named an unregistered target (#392). Defaults to
|
|
14
|
+
* `console.warn`. Falling back is correct; doing it silently is how an execution placed
|
|
15
|
+
* on sized compute ends up running somewhere else without anyone noticing.
|
|
16
|
+
*/
|
|
17
|
+
onWarn?: (message: string) => void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Policy-driven placement (Loom 2.0 M4). A composite {@link ExecutionDispatcher} that
|
|
21
|
+
* routes each dispatch to one of several named targets according to a
|
|
22
|
+
* {@link PlacementPolicy} — the WHERE decision, driven by `ExecutionPolicy`
|
|
23
|
+
* (`dispatchMetadataFromPolicy`). It owns no cloud knowledge: every target is just
|
|
24
|
+
* another ExecutionDispatcher, so local / remote-HTTP / Cloud Run workers compose
|
|
25
|
+
* behind one seam. With a single target it behaves exactly like that target, so
|
|
26
|
+
* existing single-placement dispatch is unchanged.
|
|
27
|
+
*/
|
|
28
|
+
export declare class PlacementDispatcher implements ExecutionDispatcher {
|
|
29
|
+
private readonly targets;
|
|
30
|
+
private readonly names;
|
|
31
|
+
private readonly defaultTarget;
|
|
32
|
+
private readonly policy;
|
|
33
|
+
private static readonly ROUTED_CAP;
|
|
34
|
+
private readonly routed;
|
|
35
|
+
private inFlight;
|
|
36
|
+
private readonly onWarn;
|
|
37
|
+
private readonly warnedTargets;
|
|
38
|
+
constructor(options: PlacementDispatcherOptions);
|
|
39
|
+
/** The target names currently registered. */
|
|
40
|
+
get targetNames(): readonly string[];
|
|
41
|
+
dispatch(pkg: ExecutionPackage, meta?: DispatchMetadata): Promise<ExecutionResult>;
|
|
42
|
+
cancel(executionId: string): Promise<void>;
|
|
43
|
+
health(): Promise<DispatcherHealth>;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=PlacementDispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlacementDispatcher.d.ts","sourceRoot":"","sources":["../src/PlacementDispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAElF,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EAChB,MAAM,SAAS,CAAC;AAEjB,MAAM,WAAW,0BAA0B;IACzC;;yCAEqC;IACrC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;IAC7C,4FAA4F;IAC5F,OAAO,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACpC;AAED;;;;;;;;GAQG;AACH,qBAAa,mBAAoB,YAAW,mBAAmB;IAC7D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;IAC9D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAW;IACjC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IAGzC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAU;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6B;IACpD,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA4B;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;gBAEvC,OAAO,EAAE,0BAA0B;IAc/C,6CAA6C;IAC7C,IAAI,WAAW,IAAI,SAAS,MAAM,EAAE,CAEnC;IAEK,QAAQ,CAAC,GAAG,EAAE,gBAAgB,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC;IAkClF,MAAM,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY1C,MAAM,IAAI,OAAO,CAAC,gBAAgB,CAAC;CAU1C"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PlacementDispatcher = void 0;
|
|
4
|
+
const placement_1 = require("./placement");
|
|
5
|
+
/**
|
|
6
|
+
* Policy-driven placement (Loom 2.0 M4). A composite {@link ExecutionDispatcher} that
|
|
7
|
+
* routes each dispatch to one of several named targets according to a
|
|
8
|
+
* {@link PlacementPolicy} — the WHERE decision, driven by `ExecutionPolicy`
|
|
9
|
+
* (`dispatchMetadataFromPolicy`). It owns no cloud knowledge: every target is just
|
|
10
|
+
* another ExecutionDispatcher, so local / remote-HTTP / Cloud Run workers compose
|
|
11
|
+
* behind one seam. With a single target it behaves exactly like that target, so
|
|
12
|
+
* existing single-placement dispatch is unchanged.
|
|
13
|
+
*/
|
|
14
|
+
class PlacementDispatcher {
|
|
15
|
+
targets;
|
|
16
|
+
names;
|
|
17
|
+
defaultTarget;
|
|
18
|
+
policy;
|
|
19
|
+
// Remember where each execution was placed so cancel() reaches the right target.
|
|
20
|
+
// Bounded (LRU-by-insertion) so it can't grow without limit in a long-lived dispatcher.
|
|
21
|
+
static ROUTED_CAP = 10_000;
|
|
22
|
+
routed = new Map();
|
|
23
|
+
inFlight = 0;
|
|
24
|
+
onWarn;
|
|
25
|
+
warnedTargets = new Set();
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.names = Object.keys(options.targets);
|
|
28
|
+
if (this.names.length === 0) {
|
|
29
|
+
throw new Error("PlacementDispatcher requires at least one target");
|
|
30
|
+
}
|
|
31
|
+
if (!(options.default in options.targets)) {
|
|
32
|
+
throw new Error(`PlacementDispatcher default target "${options.default}" is not registered`);
|
|
33
|
+
}
|
|
34
|
+
this.targets = options.targets;
|
|
35
|
+
this.defaultTarget = options.default;
|
|
36
|
+
this.policy = options.policy ?? (0, placement_1.defaultPlacementPolicy)(options.default);
|
|
37
|
+
this.onWarn = options.onWarn ?? ((m) => console.warn(`[loom] ${m}`));
|
|
38
|
+
}
|
|
39
|
+
/** The target names currently registered. */
|
|
40
|
+
get targetNames() {
|
|
41
|
+
return this.names;
|
|
42
|
+
}
|
|
43
|
+
async dispatch(pkg, meta) {
|
|
44
|
+
// #392 — a placement hint can outlive the lane it names: an execution suspended for days
|
|
45
|
+
// wakes into a deployment where its target was renamed or retired. Falling back is
|
|
46
|
+
// right (a retired lane must not strand what named it) but doing it SILENTLY means an
|
|
47
|
+
// execution deliberately placed on sized compute quietly runs somewhere else forever.
|
|
48
|
+
// Warned once per unknown name — this is a deployment fact, not a per-dispatch event.
|
|
49
|
+
if (meta?.target !== undefined && !this.names.includes(meta.target)) {
|
|
50
|
+
if (!this.warnedTargets.has(meta.target)) {
|
|
51
|
+
this.warnedTargets.add(meta.target);
|
|
52
|
+
this.onWarn(`placement target "${meta.target}" is not registered [${this.names.join(", ")}] — falling back to "${this.defaultTarget}". An execution that named it is now running somewhere else.`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const chosen = this.policy(pkg, meta, this.names);
|
|
56
|
+
const name = chosen in this.targets ? chosen : this.defaultTarget;
|
|
57
|
+
const target = this.targets[name];
|
|
58
|
+
this.routed.set(pkg.executionId, name);
|
|
59
|
+
// Bound `routed` (it would otherwise grow one entry per execution forever). Recent
|
|
60
|
+
// executions — the ones a cancel can still act on — keep their precise target; only a
|
|
61
|
+
// very old execution (evicted here) falls back to the all-targets cancel path, which is
|
|
62
|
+
// harmless (a finished execution ignores a stray cancel). Insertion-ordered eviction.
|
|
63
|
+
if (this.routed.size > PlacementDispatcher.ROUTED_CAP) {
|
|
64
|
+
const oldest = this.routed.keys().next().value;
|
|
65
|
+
if (oldest !== undefined)
|
|
66
|
+
this.routed.delete(oldest);
|
|
67
|
+
}
|
|
68
|
+
this.inFlight++;
|
|
69
|
+
try {
|
|
70
|
+
return await target.dispatch(pkg, meta);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
this.inFlight--;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async cancel(executionId) {
|
|
77
|
+
const name = this.routed.get(executionId);
|
|
78
|
+
if (name !== undefined) {
|
|
79
|
+
await this.targets[name].cancel(executionId);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// Placement unknown (never dispatched here, or restarted): ask every target.
|
|
83
|
+
await Promise.all(this.names.map((n) => this.targets[n].cancel(executionId)));
|
|
84
|
+
}
|
|
85
|
+
async health() {
|
|
86
|
+
const healths = await Promise.all(this.names.map((n) => this.targets[n].health()));
|
|
87
|
+
return {
|
|
88
|
+
healthy: healths.every((h) => h.healthy),
|
|
89
|
+
inFlight: this.inFlight,
|
|
90
|
+
detail: this.names.map((n, i) => `${n}:${healths[i]?.inFlight ?? 0}`).join(","),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.PlacementDispatcher = PlacementDispatcher;
|
|
95
|
+
//# sourceMappingURL=PlacementDispatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlacementDispatcher.js","sourceRoot":"","sources":["../src/PlacementDispatcher.ts"],"names":[],"mappings":";;;AACA,2CAAqD;AAyBrD;;;;;;;;GAQG;AACH,MAAa,mBAAmB;IACb,OAAO,CAAsC;IAC7C,KAAK,CAAW;IAChB,aAAa,CAAS;IACtB,MAAM,CAAkB;IACzC,iFAAiF;IACjF,wFAAwF;IAChF,MAAM,CAAU,UAAU,GAAG,MAAM,CAAC;IAC3B,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC5C,QAAQ,GAAG,CAAC,CAA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