@kindgi/flow 0.0.0-bootstrap.0 → 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 +201 -0
- package/README.md +262 -2
- package/dist/define.d.ts +36 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +29 -0
- package/dist/define.js.map +1 -0
- package/dist/errors.d.ts +252 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +4 -0
- package/dist/errors.js.map +1 -0
- package/dist/evaluator.d.ts +69 -0
- package/dist/evaluator.d.ts.map +1 -0
- package/dist/evaluator.js +220 -0
- package/dist/evaluator.js.map +1 -0
- package/dist/flow.schema.json +517 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +43 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +1051 -0
- package/dist/loader.js.map +1 -0
- package/dist/scheduler.d.ts +91 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +187 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/types.d.ts +792 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +71 -0
- package/dist/types.js.map +1 -0
- package/package.json +51 -4
- package/src/define.ts +44 -0
- package/src/errors.ts +306 -0
- package/src/evaluator.ts +254 -0
- package/src/flow.schema.json +517 -0
- package/src/index.ts +85 -0
- package/src/loader.ts +1382 -0
- package/src/scheduler.ts +274 -0
- package/src/types.ts +853 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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 describing the origin of the Work and
|
|
141
|
+
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 2026 Kindgi Inc.
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,263 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@kindgi/flow`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Flow type primitives, JSON-Schema validation, edge predicate evaluator, and scheduler-tick primitive for Kindgi. Consumed by the Kindgi runtime's kernel and any tool that authors, validates, or reasons about durable task flows.
|
|
4
|
+
|
|
5
|
+
## Exports
|
|
6
|
+
|
|
7
|
+
- **`defineFlow(spec)`** — authoring surface. Thin wrapper over `loadFlow` that mirrors `defineTool` / `defineCheck` / `defineAgent`; the recommended entry point for pack authors declaring flows in TypeScript. Returns `Result<Flow, FlowError>`.
|
|
8
|
+
- **`loadFlow(input)`** — validates against `flow.schema.json` + runs cross-cutting checks (reserved and duplicate ids, edge references, predicates, edge policy bounds, loop / fanout / subgraph rules, data-flow mappings, start edge, cycle detection, loop-body integrity). Returns `Result<Flow, FlowError>`. Kept as the wire-form entry point for JSON coming off disk / network. `GRAPH_SCHEMA_URI` is the `$id` it validates against.
|
|
9
|
+
- **`evaluateExpr(expr, env)`** — deterministic boolean evaluator over the closed Expr DSL.
|
|
10
|
+
- **`resolveMapping(mapping, env)`** — resolves a `Mapping` (leaf or subgraph `inputMapping`, flow `output.mapping`) against an `EvalEnv`; a path that doesn't resolve omits its key. `resolvePath(path, env)` resolves one path; `MISSING_PATH` is the marker it returns when the path doesn't resolve.
|
|
11
|
+
- **`schedulerTick(flow, state)`** — pure per-tick output (`{ ready, edgeEvals, done }`) from a `SchedulerState`.
|
|
12
|
+
- Type exports: `Flow`, `FlowSpec`, `FlowEdge`, `FlowNode`, `FlowOutputSpec`, `LeafNode`, `WhileLoopNode`, `ForeachLoopNode`, `LoopNode`, `LoopBody`, `LoopEdge`, `FanoutNode`, `FanoutBranch`, `ConvergenceMode`, `SubflowNode`, `SubflowRef`, `SubflowConvergence`, `Mapping`, `Expr`, `Operand`, `LiteralOperand`, `PathOperand`, `Path`, `PathRoot`, `OperatorName`, `LoopOutputSchema`, `EdgePolicy`, `RetryPolicy`, `BackoffShape`, `EvalEnv`, `SchedulerState`, `SchedulerTick`, `PendingEdgeEval`, and `FlowError` with one interface per error code.
|
|
13
|
+
- Value exports: `EDGE_POLICY_PRIORITY_DEFAULT` (0), `EDGE_POLICY_PRIORITY_MIN` (-100), `EDGE_POLICY_PRIORITY_MAX` (100), `getEffectivePriority(edgesByTo, nodeId)` (resolves the effective scheduler priority for a node, applying the fan-in default rule), `OPERATORS`, `CONVERGENCE_MODES`, `SUBGRAPH_CONVERGENCE_MODES`, the sentinels `START_NODE` / `END_NODE` / `LOOP_START_NODE` / `LOOP_END_NODE` / `SENTINEL_IDS`, and the type guards `isLeafNode` / `isLoopNode` / `isFanoutNode` / `isSubflowNode`.
|
|
14
|
+
|
|
15
|
+
## Authoring
|
|
16
|
+
|
|
17
|
+
Two entry points for building a `Flow`, both returning the same `Result<Flow, FlowError>` after the same validation gauntlet:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { defineFlow, loadFlow } from '@kindgi/flow';
|
|
21
|
+
import type { FlowSpec } from '@kindgi/flow';
|
|
22
|
+
|
|
23
|
+
// Recommended for pack authors — mirrors defineTool / defineCheck / defineAgent.
|
|
24
|
+
const flow = defineFlow({
|
|
25
|
+
id: 'my.flow',
|
|
26
|
+
version: '1.0.0',
|
|
27
|
+
nodes: [{ id: 'step-a', kind: 'tool', ref: 'noop' }],
|
|
28
|
+
edges: [
|
|
29
|
+
{ id: 'e1', from: '$start', to: 'step-a' },
|
|
30
|
+
{ id: 'e2', from: 'step-a', to: '$end' },
|
|
31
|
+
],
|
|
32
|
+
} satisfies FlowSpec);
|
|
33
|
+
|
|
34
|
+
// Same validation surface — use loadFlow when the input is untrusted wire JSON.
|
|
35
|
+
const loaded = loadFlow(JSON.parse(bytesFromDisk));
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`defineFlow` is byte-for-byte equivalent to `loadFlow` on the same input; the two share every check. Pick `defineFlow` when authoring in TypeScript to align with the other `define*` primitives; pick `loadFlow` when consuming flow JSON from disk, network, or an untrusted caller.
|
|
39
|
+
|
|
40
|
+
## Retry policy (schema-version 1.3.0+)
|
|
41
|
+
|
|
42
|
+
Per-edge retry is declared via `FlowEdge.policy.retry`. The kernel honors this policy only when the destination node has EXACTLY ONE incoming edge — fan-in destinations (2+ incoming edges) ignore retry policies (there is no rule for whose policy would win).
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import type { FlowEdge, RetryPolicy } from '@kindgi/flow';
|
|
46
|
+
|
|
47
|
+
const retry: RetryPolicy = {
|
|
48
|
+
maxAttempts: 5, // 1..10; 1 = no retry
|
|
49
|
+
delayMs: 100, // ≥ 0; default 0
|
|
50
|
+
backoff: 'exponential', // 'fixed' | 'linear' | 'exponential'; default 'fixed'
|
|
51
|
+
maxDelayMs: 5_000, // required when backoff = 'exponential'
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const edge: FlowEdge = {
|
|
55
|
+
id: 'e1',
|
|
56
|
+
from: 'produce',
|
|
57
|
+
to: 'consume',
|
|
58
|
+
policy: { retry },
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Valid ranges (enforced by `loadFlow`)
|
|
63
|
+
|
|
64
|
+
| Field | Range | Notes |
|
|
65
|
+
|---|---|---|
|
|
66
|
+
| `maxAttempts` | `1..10` (integer) | Larger values are a design smell — retry-heavy control flow belongs in a supervising flow. |
|
|
67
|
+
| `delayMs` | `≥ 0` (integer) | Default `0`. |
|
|
68
|
+
| `backoff` | `'fixed' \| 'linear' \| 'exponential'` | Default `'fixed'`. |
|
|
69
|
+
| `maxDelayMs` | `≥ 0` (integer) | REQUIRED when `backoff = 'exponential'`. Defaults to `60_000` for `linear`. |
|
|
70
|
+
|
|
71
|
+
Loader errors: `invalid-edge-policy` (with `field: 'retry.maxAttempts' | 'retry.delayMs' | 'retry.backoff' | 'retry.maxDelayMs'` and a `reason` string) for semantic violations; `schema-validation-failed` for shape violations.
|
|
72
|
+
|
|
73
|
+
### Delay formula
|
|
74
|
+
|
|
75
|
+
For the `attempt`-th retry (1-based; `attempt = 1` is the first retry after the initial failure):
|
|
76
|
+
|
|
77
|
+
| Shape | Formula |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `fixed` | `delayMs` |
|
|
80
|
+
| `linear` | `min(delayMs * attempt, maxDelayMs ?? 60_000)` |
|
|
81
|
+
| `exponential` | `min(delayMs * 2^(attempt - 1), maxDelayMs)` |
|
|
82
|
+
|
|
83
|
+
Kernel runtime semantics + journal shape (`step.retry-scheduled`, resume behaviour, cancellation) are implemented by the Kindgi runtime's kernel.
|
|
84
|
+
|
|
85
|
+
## Timeout policy (schema-version 1.3.0+)
|
|
86
|
+
|
|
87
|
+
Per-edge handler-invocation timeout is declared via `FlowEdge.policy.timeoutMs`. Same fan-in rule as retry: applies only to a destination node with EXACTLY ONE incoming edge.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const edge: FlowEdge = {
|
|
91
|
+
id: 'e1',
|
|
92
|
+
from: 'produce',
|
|
93
|
+
to: 'consume',
|
|
94
|
+
policy: { timeoutMs: 5_000 },
|
|
95
|
+
};
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Valid ranges (enforced by `loadFlow`)
|
|
99
|
+
|
|
100
|
+
| Field | Range | Notes |
|
|
101
|
+
|---|---|---|
|
|
102
|
+
| `timeoutMs` | `1..3_600_000` (integer) | 1 hour cap. Longer work belongs behind `ctx.waitForToken` or in a `foreach` with `concurrency`. |
|
|
103
|
+
|
|
104
|
+
Loader errors: `invalid-edge-policy` with `field: 'timeoutMs'` for out-of-band values; `schema-validation-failed` for shape violations.
|
|
105
|
+
|
|
106
|
+
Kernel runtime semantics (timer wiring, `AbortSignal` composition, timeout+retry interaction, cancel+timeout attribution, loop-node bounding) are implemented by the Kindgi runtime's kernel.
|
|
107
|
+
|
|
108
|
+
## Concurrency policy (schema-version 1.4.0+)
|
|
109
|
+
|
|
110
|
+
Per-edge kernel semaphore is declared via `FlowEdge.policy.concurrencyKey`. When set on the sole incoming edge of a destination node, the kernel serializes dispatch across every node (across every run, across every flow, within a single tenant) that shares the same key: at most one such node runs at a time.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const edge: FlowEdge = {
|
|
114
|
+
id: 'e-model',
|
|
115
|
+
from: '$start',
|
|
116
|
+
to: 'call-model',
|
|
117
|
+
policy: { concurrencyKey: 'per-tenant:model-api' },
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Valid ranges (enforced by `loadFlow`)
|
|
122
|
+
|
|
123
|
+
| Field | Range | Notes |
|
|
124
|
+
|---|---|---|
|
|
125
|
+
| `concurrencyKey` | 1..256 chars, printable ASCII (0x20..0x7E) | Coordination handle, not a display label. Non-printable / non-ASCII rejected. |
|
|
126
|
+
|
|
127
|
+
Loader errors: `invalid-edge-policy` with `field: 'concurrencyKey'` for length / character-set violations; `schema-validation-failed` for shape violations.
|
|
128
|
+
|
|
129
|
+
Kernel runtime semantics (lease acquire/release lifecycle, `step.concurrency-deferred` journal kind, waitForToken suspend-release / resume-reacquire, retry re-acquire, sweep on resume) are implemented by the Kindgi runtime's kernel.
|
|
130
|
+
|
|
131
|
+
## Priority policy (schema-version 1.5.0+)
|
|
132
|
+
|
|
133
|
+
Per-edge scheduler priority is declared via `FlowEdge.policy.priority`. Same fan-in rule as retry / timeoutMs / concurrencyKey: applies only to a destination node with EXACTLY ONE incoming edge. Fan-in nodes inherit the default (`0`).
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
const highPriority: FlowEdge = {
|
|
137
|
+
id: 'e-urgent',
|
|
138
|
+
from: '$start',
|
|
139
|
+
to: 'expedite',
|
|
140
|
+
policy: { priority: 90 },
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const background: FlowEdge = {
|
|
144
|
+
id: 'e-lazy',
|
|
145
|
+
from: '$start',
|
|
146
|
+
to: 'cleanup',
|
|
147
|
+
policy: { priority: -20 },
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Valid ranges (enforced by `loadFlow`)
|
|
152
|
+
|
|
153
|
+
| Field | Range | Notes |
|
|
154
|
+
|---|---|---|
|
|
155
|
+
| `priority` | `-100..100` (integer) | Default `0`. Integer-only: floats introduce IEEE-754 comparison ambiguity in a replay-critical sort key. Bounded range because the primitive expresses tiers ("urgent" / "normal" / "background") plus finer-grained ordering — wider ranges add no expressive power. |
|
|
156
|
+
|
|
157
|
+
Loader errors: `invalid-edge-policy` with `field: 'priority'` for range / integer / type violations; `schema-validation-failed` for shape violations.
|
|
158
|
+
|
|
159
|
+
Kernel runtime semantics (dispatch ordering, maxParallelism interaction, concurrencyKey interaction, determinism) are implemented by the Kindgi runtime's kernel.
|
|
160
|
+
|
|
161
|
+
### `EdgePolicy` is a closed surface
|
|
162
|
+
|
|
163
|
+
`retry` / `timeoutMs` / `concurrencyKey` / `priority` are the complete honored set. There is no `suspend` field — declarative suspension uses `ctx.waitForToken` at handler level. The schema enforces `additionalProperties: false`, so unknown edge-policy keys are rejected at load time.
|
|
164
|
+
|
|
165
|
+
## Loop primitive
|
|
166
|
+
|
|
167
|
+
Loop node types + `outputSchema` well-formedness live here; the runtime lives in the Kindgi runtime's kernel.
|
|
168
|
+
|
|
169
|
+
## Fanout primitive (schema-version 1.6.0+)
|
|
170
|
+
|
|
171
|
+
`FanoutNode` is a first-class node kind that runs N different handler bodies in parallel from a single dispatch, converging by a declared mode. Complementary to `foreach` (N iterations of the SAME body), fanout runs N DIFFERENT bodies concurrently.
|
|
172
|
+
|
|
173
|
+
Shape:
|
|
174
|
+
|
|
175
|
+
```jsonc
|
|
176
|
+
{
|
|
177
|
+
"id": "the-fanout",
|
|
178
|
+
"kind": "fanout",
|
|
179
|
+
"branches": [ // >= 2, unique branchIds
|
|
180
|
+
{ "branchId": "a", "handler": "h-a", "outputSchema": { /* draft 2020-12 */ } },
|
|
181
|
+
{ "branchId": "b", "handler": "h-b", "outputSchema": { /* draft 2020-12 */ } }
|
|
182
|
+
],
|
|
183
|
+
"concurrency": 2, // optional, default = branches.length; 1..branches.length
|
|
184
|
+
"convergence": "settle-all" // 'all-succeed' | 'any-succeed' | 'settle-all'
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Loader-enforced guardrails (`invalid-fanout-node`):
|
|
189
|
+
|
|
190
|
+
- `branches.length >= 2` (a single-branch fanout is just a leaf node).
|
|
191
|
+
- `branchId` non-empty + unique within a single fanout node.
|
|
192
|
+
- `handler` non-empty (opaque to the loader — the kernel resolves it against the HandlerRegistry).
|
|
193
|
+
- Per-branch `outputSchema` present + compiles against the draft-2020-12 meta-schema.
|
|
194
|
+
- `concurrency` (when set) integer in `[1, branches.length]`.
|
|
195
|
+
- `convergence` in the closed enum `{ 'all-succeed', 'any-succeed', 'settle-all' }`.
|
|
196
|
+
|
|
197
|
+
Kernel runtime semantics (dispatch, sibling cancellation, replay, fan-in shape per convergence) are implemented by the Kindgi runtime's kernel.
|
|
198
|
+
|
|
199
|
+
## Subgraph primitive (schema-version 1.7.0+)
|
|
200
|
+
|
|
201
|
+
`SubflowNode` is a first-class node kind that dispatches another registered flow as a full kernel sub-run. Complementary to loop + fanout: `loop.foreach` iterates the SAME body over N inputs; `fanout` runs N DIFFERENT bodies in parallel; `subgraph` runs a whole OTHER flow inline as a node.
|
|
202
|
+
|
|
203
|
+
Shape:
|
|
204
|
+
|
|
205
|
+
```jsonc
|
|
206
|
+
{
|
|
207
|
+
"id": "the-subgraph",
|
|
208
|
+
"kind": "subgraph",
|
|
209
|
+
"flowRef": {
|
|
210
|
+
"flowId": "child.flow.id",
|
|
211
|
+
"version": "1.2.3" // EXACT semver (no ranges)
|
|
212
|
+
},
|
|
213
|
+
"inputMapping": { // Record<string, Operand>
|
|
214
|
+
"target": { "path": "runInput.target" },
|
|
215
|
+
"hint": { "literal": "expedite" }
|
|
216
|
+
},
|
|
217
|
+
"outputSchema": { /* draft 2020-12 */ }, // typed escape contract
|
|
218
|
+
"convergence": "settle-all" // 'success-only' | 'settle-all'
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Loader-enforced guardrails (`invalid-subgraph-node`):
|
|
223
|
+
|
|
224
|
+
- `flowRef.flowId` non-empty.
|
|
225
|
+
- `flowRef.version` is EXACT semver (`^` / `~` / ranges rejected — deterministic replay requires exact resolution).
|
|
226
|
+
- `inputMapping` is a plain object whose every value is a valid `Operand` (`{ path }` or `{ literal }`).
|
|
227
|
+
- `outputSchema` present + compiles against the draft-2020-12 meta-schema (same discipline as loop / fanout output schemas).
|
|
228
|
+
- `convergence` in the closed enum `{ 'success-only', 'settle-all' }`.
|
|
229
|
+
|
|
230
|
+
Kernel runtime semantics (child-run dispatch, journal shape, replay via prior-dispatch reattach, cross-tenant guard, depth-limit enforcement) are implemented by the Kindgi runtime's kernel.
|
|
231
|
+
|
|
232
|
+
## Data flow (schema-version 1.8.0+)
|
|
233
|
+
|
|
234
|
+
A tool or agent node can declare an `inputMapping`, and the flow can declare an `output`. Both are `Mapping`s: each key maps to a `{ literal }` or a `{ path }` rooted at `runInput`, `state` or `nodeOutputs.<nodeId>`, resolved with `resolveMapping`. A path that doesn't resolve omits its key.
|
|
235
|
+
|
|
236
|
+
```jsonc
|
|
237
|
+
{
|
|
238
|
+
"nodes": [
|
|
239
|
+
{ "id": "parse", "kind": "tool", "ref": "acme.parse-profile" },
|
|
240
|
+
{
|
|
241
|
+
"id": "rank",
|
|
242
|
+
"kind": "tool",
|
|
243
|
+
"ref": "acme.rank",
|
|
244
|
+
"inputMapping": { // absent → the single upstream node's output
|
|
245
|
+
"profile": { "path": "nodeOutputs.parse" },
|
|
246
|
+
"orgId": { "path": "runInput.orgId" }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
],
|
|
250
|
+
"output": { // absent → the output of the node feeding $end
|
|
251
|
+
"mapping": { "ranking": { "path": "nodeOutputs.rank" } },
|
|
252
|
+
"schema": { "type": "object" } // optional; must compile
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Loader errors: `invalid-mapping` when a mapping names a node the flow doesn't have, maps a node's own output, or uses a loop-only root (`iterationIndex` / `iterationOutput`); `invalid-flow-output` when `output.schema` doesn't compile; `schema-validation-failed` for a path with an unknown root.
|
|
258
|
+
|
|
259
|
+
A conditional branch can rejoin: `schedulerTick` treats a node whose every incoming edge is not taken, or comes from such a skipped node, as dead. A join after a skipped arm becomes ready once its taken arms resolve, and a join whose arms are all skipped is skipped too.
|
|
260
|
+
|
|
261
|
+
## Schema drift
|
|
262
|
+
|
|
263
|
+
`src/flow.schema.json` is bundled into the package for offline validation. It must match `@kindgi/specs/flow.schema.json` (a test compares the parsed JSON); the two change together.
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Result, Unbranded } from '@kindgi/types';
|
|
2
|
+
import type { FlowError } from './errors.js';
|
|
3
|
+
import type { Flow } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* The author-facing spec accepted by `defineFlow`: the shape of the runtime
|
|
6
|
+
* `Flow` — an object literal conforming to `flow.schema.json` — with its
|
|
7
|
+
* ids as plain strings. An author writes `id: 'acme.review'`, a node
|
|
8
|
+
* `id: 'parse'`, an edge `from: 'parse'`, with no `as FlowId` / `as NodeId`
|
|
9
|
+
* / `as EdgeId` casts; branded ids still fit. `defineFlow` returns the
|
|
10
|
+
* validated `Flow`, branded, ready for the kernel or a registry.
|
|
11
|
+
*/
|
|
12
|
+
export type FlowSpec = Unbranded<Flow>;
|
|
13
|
+
/**
|
|
14
|
+
* Build a validated `Flow` from a user-supplied definition.
|
|
15
|
+
*
|
|
16
|
+
* Thin wrapper over `loadFlow` — same validation gauntlet, same error
|
|
17
|
+
* surface, same `Result<Flow, FlowError>` envelope. The distinction is
|
|
18
|
+
* ergonomic: `defineFlow` is the author-facing primitive that mirrors
|
|
19
|
+
* `defineTool` / `defineCheck` / `defineAgent`, so pack authors reach for
|
|
20
|
+
* one consistent shape when declaring workspace primitives. `loadFlow`
|
|
21
|
+
* remains the wire-form entry point for JSON coming off disk / network.
|
|
22
|
+
*
|
|
23
|
+
* Validates (fail-fast, in `loadFlow` order):
|
|
24
|
+
* 1. JSON-schema conformance against `flow.schema.json`.
|
|
25
|
+
* 2. Reserved-id / duplicate-id / unknown-node-reference checks.
|
|
26
|
+
* 3. Predicate + edge-policy + loop / fanout / subgraph well-formedness,
|
|
27
|
+
* data-flow mappings and the flow `output.schema`.
|
|
28
|
+
* 4. Start-edge check and cycle detection (outer + every loop body at
|
|
29
|
+
* every nesting level).
|
|
30
|
+
*
|
|
31
|
+
* The returned `Flow` is byte-identical to `loadFlow(spec).value` on
|
|
32
|
+
* success; consumers can treat the two entry points as interchangeable
|
|
33
|
+
* modulo authoring intent.
|
|
34
|
+
*/
|
|
35
|
+
export declare function defineFlow(spec: FlowSpec): Result<Flow, FlowError>;
|
|
36
|
+
//# sourceMappingURL=define.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.d.ts","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,CAElE"}
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
import { loadFlow } from './loader.js';
|
|
4
|
+
/**
|
|
5
|
+
* Build a validated `Flow` from a user-supplied definition.
|
|
6
|
+
*
|
|
7
|
+
* Thin wrapper over `loadFlow` — same validation gauntlet, same error
|
|
8
|
+
* surface, same `Result<Flow, FlowError>` envelope. The distinction is
|
|
9
|
+
* ergonomic: `defineFlow` is the author-facing primitive that mirrors
|
|
10
|
+
* `defineTool` / `defineCheck` / `defineAgent`, so pack authors reach for
|
|
11
|
+
* one consistent shape when declaring workspace primitives. `loadFlow`
|
|
12
|
+
* remains the wire-form entry point for JSON coming off disk / network.
|
|
13
|
+
*
|
|
14
|
+
* Validates (fail-fast, in `loadFlow` order):
|
|
15
|
+
* 1. JSON-schema conformance against `flow.schema.json`.
|
|
16
|
+
* 2. Reserved-id / duplicate-id / unknown-node-reference checks.
|
|
17
|
+
* 3. Predicate + edge-policy + loop / fanout / subgraph well-formedness,
|
|
18
|
+
* data-flow mappings and the flow `output.schema`.
|
|
19
|
+
* 4. Start-edge check and cycle detection (outer + every loop body at
|
|
20
|
+
* every nesting level).
|
|
21
|
+
*
|
|
22
|
+
* The returned `Flow` is byte-identical to `loadFlow(spec).value` on
|
|
23
|
+
* success; consumers can treat the two entry points as interchangeable
|
|
24
|
+
* modulo authoring intent.
|
|
25
|
+
*/
|
|
26
|
+
export function defineFlow(spec) {
|
|
27
|
+
return loadFlow(spec);
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=define.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.js","sourceRoot":"","sources":["../src/define.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAKjC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAavC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,UAAU,CAAC,IAAc;IACvC,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC"}
|