@titan-design/hitl 0.4.0 → 0.6.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/NOTICE +216 -0
- package/README.md +61 -1
- package/dist/{base-store-BrVibYRJ.d.ts → base-store-B-IPO0UN.d.ts} +55 -3
- package/dist/{chunk-PMEJTMSE.js → chunk-TE2NXWWK.js} +149 -7
- package/dist/chunk-TE2NXWWK.js.map +1 -0
- package/dist/index.d.ts +34 -3
- package/dist/index.js +13 -3
- package/dist/index.js.map +1 -1
- package/dist/sqlite.d.ts +14 -7
- package/dist/sqlite.js +50 -27
- package/dist/sqlite.js.map +1 -1
- package/package.json +5 -4
- package/dist/chunk-PMEJTMSE.js.map +0 -1
package/NOTICE
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
@titan-design/hitl
|
|
2
|
+
Copyright 2026 Henry Jewkes. Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
This package includes code adapted from openrig
|
|
5
|
+
(https://github.com/mvschwarz/openrig), licensed under the Apache License 2.0.
|
|
6
|
+
|
|
7
|
+
Upstream file: packages/daemon/src/domain/human-questions.ts (parseHumanQuestions)
|
|
8
|
+
Adapted in: src/gate-brief.ts (the question bounds and questionsIssue)
|
|
9
|
+
Copyright 2026 Mike Schwarz
|
|
10
|
+
|
|
11
|
+
Changes: the checks report through GateBriefInvalid instead of a result union,
|
|
12
|
+
and `recommended` keeps only `true`.
|
|
13
|
+
|
|
14
|
+
The full text of the Apache License 2.0 follows.
|
|
15
|
+
|
|
16
|
+
Apache License
|
|
17
|
+
Version 2.0, January 2004
|
|
18
|
+
http://www.apache.org/licenses/
|
|
19
|
+
|
|
20
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
21
|
+
|
|
22
|
+
1. Definitions.
|
|
23
|
+
|
|
24
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
25
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
26
|
+
|
|
27
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
28
|
+
the copyright owner that is granting the License.
|
|
29
|
+
|
|
30
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
31
|
+
other entities that control, are controlled by, or are under common
|
|
32
|
+
control with that entity. For the purposes of this definition,
|
|
33
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
34
|
+
direction or management of such entity, whether by contract or
|
|
35
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
36
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
37
|
+
|
|
38
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
39
|
+
exercising permissions granted by this License.
|
|
40
|
+
|
|
41
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
42
|
+
including but not limited to software source code, documentation
|
|
43
|
+
source, and configuration files.
|
|
44
|
+
|
|
45
|
+
"Object" form shall mean any form resulting from mechanical
|
|
46
|
+
transformation or translation of a Source form, including but
|
|
47
|
+
not limited to compiled object code, generated documentation,
|
|
48
|
+
and conversions to other media types.
|
|
49
|
+
|
|
50
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
51
|
+
Object form, made available under the License, as indicated by a
|
|
52
|
+
copyright notice that is included in or attached to the work
|
|
53
|
+
(an example is provided in the Appendix below).
|
|
54
|
+
|
|
55
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
56
|
+
form, that is based on (or derived from) the Work and for which the
|
|
57
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
58
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
59
|
+
of this License, Derivative Works shall not include works that remain
|
|
60
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
61
|
+
the Work and Derivative Works thereof.
|
|
62
|
+
|
|
63
|
+
"Contribution" shall mean any work of authorship, including
|
|
64
|
+
the original version of the Work and any modifications or additions
|
|
65
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
66
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
67
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
68
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
69
|
+
means any form of electronic, verbal, or written communication sent
|
|
70
|
+
to the Licensor or its representatives, including but not limited to
|
|
71
|
+
communication on electronic mailing lists, source code control systems,
|
|
72
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
73
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
74
|
+
excluding communication that is conspicuously marked or otherwise
|
|
75
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
76
|
+
|
|
77
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
78
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
79
|
+
subsequently incorporated within the Work.
|
|
80
|
+
|
|
81
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
82
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
83
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
84
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
85
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
86
|
+
Work and such Derivative Works in Source or Object form.
|
|
87
|
+
|
|
88
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
89
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
90
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
91
|
+
(except as stated in this section) patent license to make, have made,
|
|
92
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
93
|
+
where such license applies only to those patent claims licensable
|
|
94
|
+
by such Contributor that are necessarily infringed by their
|
|
95
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
96
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
97
|
+
institute patent litigation against any entity (including a
|
|
98
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
99
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
100
|
+
or contributory patent infringement, then any patent licenses
|
|
101
|
+
granted to You under this License for that Work shall terminate
|
|
102
|
+
as of the date such litigation is filed.
|
|
103
|
+
|
|
104
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
105
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
106
|
+
modifications, and in Source or Object form, provided that You
|
|
107
|
+
meet the following conditions:
|
|
108
|
+
|
|
109
|
+
(a) You must give any other recipients of the Work or
|
|
110
|
+
Derivative Works a copy of this License; and
|
|
111
|
+
|
|
112
|
+
(b) You must cause any modified files to carry prominent notices
|
|
113
|
+
stating that You changed the files; and
|
|
114
|
+
|
|
115
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
116
|
+
that You distribute, all copyright, patent, trademark, and
|
|
117
|
+
attribution notices from the Source form of the Work,
|
|
118
|
+
excluding those notices that do not pertain to any part of
|
|
119
|
+
the Derivative Works; and
|
|
120
|
+
|
|
121
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
122
|
+
distribution, then any Derivative Works that You distribute must
|
|
123
|
+
include a readable copy of the attribution notices contained
|
|
124
|
+
within such NOTICE file, excluding those notices that do not
|
|
125
|
+
pertain to any part of the Derivative Works, in at least one
|
|
126
|
+
of the following places: within a NOTICE text file distributed
|
|
127
|
+
as part of the Derivative Works; within the Source form or
|
|
128
|
+
documentation, if provided along with the Derivative Works; or,
|
|
129
|
+
within a display generated by the Derivative Works, if and
|
|
130
|
+
wherever such third-party notices normally appear. The contents
|
|
131
|
+
of the NOTICE file are for informational purposes only and
|
|
132
|
+
do not modify the License. You may add Your own attribution
|
|
133
|
+
notices within Derivative Works that You distribute, alongside
|
|
134
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
135
|
+
that such additional attribution notices cannot be construed
|
|
136
|
+
as modifying the License.
|
|
137
|
+
|
|
138
|
+
You may add Your own copyright statement to Your modifications and
|
|
139
|
+
may provide additional or different license terms and conditions
|
|
140
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
141
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
142
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
143
|
+
the conditions stated in this License.
|
|
144
|
+
|
|
145
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
146
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
147
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
148
|
+
this License, without any additional terms or conditions.
|
|
149
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
150
|
+
the terms of any separate license agreement you may have executed
|
|
151
|
+
with Licensor regarding such Contributions.
|
|
152
|
+
|
|
153
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
154
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
155
|
+
except as required for reasonable and customary use in describing the
|
|
156
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
157
|
+
|
|
158
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
159
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
160
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
161
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
162
|
+
implied, including, without limitation, any warranties or conditions
|
|
163
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
164
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
165
|
+
appropriateness of using or redistributing the Work and assume any
|
|
166
|
+
risks associated with Your exercise of permissions under this License.
|
|
167
|
+
|
|
168
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
169
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
170
|
+
unless required by applicable law (such as deliberate and grossly
|
|
171
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
172
|
+
liable to You for damages, including any direct, indirect, special,
|
|
173
|
+
incidental, or consequential damages of any character arising as a
|
|
174
|
+
result of this License or out of the use or inability to use the
|
|
175
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
176
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
177
|
+
other commercial damages or losses), even if such Contributor
|
|
178
|
+
has been advised of the possibility of such damages.
|
|
179
|
+
|
|
180
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
181
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
182
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
183
|
+
or other liability obligations and/or rights consistent with this
|
|
184
|
+
License. However, in accepting such obligations, You may act only
|
|
185
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
186
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
187
|
+
defend, and hold each Contributor harmless for any liability
|
|
188
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
189
|
+
of your accepting any such warranty or additional liability.
|
|
190
|
+
|
|
191
|
+
END OF TERMS AND CONDITIONS
|
|
192
|
+
|
|
193
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
194
|
+
|
|
195
|
+
To apply the Apache License to your work, attach the following
|
|
196
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
197
|
+
replaced with your own identifying information. (Don't include
|
|
198
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
199
|
+
comment syntax for the file format. We also recommend that a
|
|
200
|
+
file or class name and description of purpose be included on the
|
|
201
|
+
same "printed page" as the copyright notice for easier
|
|
202
|
+
identification within third-party archives.
|
|
203
|
+
|
|
204
|
+
Copyright [yyyy] [name of copyright owner]
|
|
205
|
+
|
|
206
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
207
|
+
you may not use this file except in compliance with the License.
|
|
208
|
+
You may obtain a copy of the License at
|
|
209
|
+
|
|
210
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
211
|
+
|
|
212
|
+
Unless required by applicable law or agreed to in writing, software
|
|
213
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
214
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
215
|
+
See the License for the specific language governing permissions and
|
|
216
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -29,12 +29,15 @@ point. The waiter polls, because the resolver may be a different process writing
|
|
|
29
29
|
the same SQLite file.
|
|
30
30
|
|
|
31
31
|
```ts
|
|
32
|
+
import os from "node:os";
|
|
33
|
+
import path from "node:path";
|
|
32
34
|
import { openDatabase } from "@titan-design/store-sqlite";
|
|
33
35
|
import { SqliteGateStore } from "@titan-design/hitl/sqlite";
|
|
34
36
|
import { openGate } from "@titan-design/hitl";
|
|
35
37
|
import { z } from "zod";
|
|
36
38
|
|
|
37
|
-
const
|
|
39
|
+
const dbPath = path.join(os.homedir(), ".local/state/thing/gates.sqlite3");
|
|
40
|
+
const store = new SqliteGateStore(openDatabase(dbPath));
|
|
38
41
|
|
|
39
42
|
const gate = openGate(store, {
|
|
40
43
|
id: "deploy-approval",
|
|
@@ -123,6 +126,19 @@ never fewer. It must return `{ allowed }` synchronously, or the store throws
|
|
|
123
126
|
`GateAuthorizeInvalid`.
|
|
124
127
|
Refusals name the gate id and the actor class, never the resolver's other fields.
|
|
125
128
|
|
|
129
|
+
### Allowances
|
|
130
|
+
|
|
131
|
+
A product may admit a non-owner class for specific answers by passing `allowances` to the store:
|
|
132
|
+
a list of `{ resolverClass, stepId, payload }`. An allowance admits exactly that class, a gate whose
|
|
133
|
+
step is `stepId` (the text after the last `/` of its id, with any `:<n>` repeat suffix removed) and a
|
|
134
|
+
payload deep-equal to `payload`, and only when the resolver's `id` is not blank. Anything else gets the
|
|
135
|
+
default refusal unchanged. The list is copied when the store is built, so a caller cannot widen it
|
|
136
|
+
later, and the gate's rule and `authorize` still run after it.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
new MemoryGateStore({ allowances: [{ resolverClass: "coordinator", stepId: "stuck-behind", payload: { decision: "retry" } }] });
|
|
140
|
+
```
|
|
141
|
+
|
|
126
142
|
hitl records a claim about the resolver; it cannot prove one. Any process that can write
|
|
127
143
|
the database can claim any class.
|
|
128
144
|
|
|
@@ -203,6 +219,50 @@ covers only pending rows. A raw `REPLACE` of a cancelled or resolved rule-bound
|
|
|
203
219
|
and can leave a pending rule-less row. That is outside the store API: `create` is a plain
|
|
204
220
|
insert and throws on an existing id, and no store method replaces a row.
|
|
205
221
|
|
|
222
|
+
## Gate briefs
|
|
223
|
+
|
|
224
|
+
A gate's `prompt` is written for the machine. A brief is what the owner reads: pass
|
|
225
|
+
`summary`, `evidenceRef` and optionally `questions` to `create` or `openGate`.
|
|
226
|
+
|
|
227
|
+
```ts
|
|
228
|
+
openGate(store, {
|
|
229
|
+
id: "release-approval",
|
|
230
|
+
prompt: "release-1.4.0",
|
|
231
|
+
schema: z.object({ decision: z.enum(["release", "hold"]) }),
|
|
232
|
+
summary: "Release 1.4.0? CI green on main. Recommend release.",
|
|
233
|
+
evidenceRef: "https://github.com/acme/thing/actions/runs/1",
|
|
234
|
+
questions: [{
|
|
235
|
+
id: "decision",
|
|
236
|
+
question: "Release 1.4.0?",
|
|
237
|
+
options: [{ id: "release", label: "Release", recommended: true }, { id: "hold", label: "Hold" }],
|
|
238
|
+
}],
|
|
239
|
+
});
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
| Field | Bound |
|
|
243
|
+
| --- | --- |
|
|
244
|
+
| `summary` | 1-280 characters on one line: the decision and the recommendation |
|
|
245
|
+
| `evidenceRef` | 1-500 characters on one line: an https URL, an absolute path, or `$ <command>` |
|
|
246
|
+
| `questions` | 1-4 questions with unique ids; each has 1-500 characters of text and 2-4 options |
|
|
247
|
+
| option | unique id (1-40 letters, digits, `-`, `_`), a label of at most 75 characters (the Slack button limit), at most one `recommended: true` per question |
|
|
248
|
+
|
|
249
|
+
A store built with `requireBrief: true` refuses `create` without a `summary` and an
|
|
250
|
+
`evidenceRef`. Without it both stay optional, but a brief field that is present is still
|
|
251
|
+
checked. A refused brief throws `GateBriefInvalid`, whose `issues` names each problem, and
|
|
252
|
+
writes no row. `questions` is the presentation contract only: `schema` still validates the
|
|
253
|
+
answer, and hitl does not cross-check the two. `snapshotBrief` is the same check, exported for
|
|
254
|
+
a surface that wants to validate before it opens a gate.
|
|
255
|
+
|
|
256
|
+
On SQLite the brief lives in `summary`, `evidence_ref` and `questions` columns that
|
|
257
|
+
`gateBriefMigration(n)` adds. It is idempotent and does not backfill: gates opened before it
|
|
258
|
+
read back with all three undefined, and still resolve. `migrate: true` runs it as version 4.
|
|
259
|
+
A store whose table lacks the columns throws `GateStoreSchemaOutdated` naming
|
|
260
|
+
`gateBriefMigration` when handed a brief, rather than dropping it, and refuses to construct
|
|
261
|
+
at all when `requireBrief` is set.
|
|
262
|
+
|
|
263
|
+
The question bounds are adapted from openrig (Apache-2.0); `src/gate-brief.ts` carries the
|
|
264
|
+
attribution.
|
|
265
|
+
|
|
206
266
|
## Settling
|
|
207
267
|
|
|
208
268
|
A gate is `pending`, then exactly one of `resolved`, `cancelled`, or `expired`.
|
|
@@ -20,6 +20,37 @@ interface GateRecord {
|
|
|
20
20
|
resolvedBy: GateResolver | undefined;
|
|
21
21
|
/** The authority rule that opened the gate; unset for a gate no rule governs. */
|
|
22
22
|
rule: GateRule | undefined;
|
|
23
|
+
/** One line for the owner: the decision and the recommendation. Unset for a gate opened without a brief. */
|
|
24
|
+
summary: string | undefined;
|
|
25
|
+
/** Where the owner checks the facts: an https URL, an absolute path, or `$ <command>`. */
|
|
26
|
+
evidenceRef: string | undefined;
|
|
27
|
+
/** A fixed menu a surface can render as buttons; the gate's `schema` still validates the answer. */
|
|
28
|
+
questions: GateQuestion[] | undefined;
|
|
29
|
+
}
|
|
30
|
+
interface GateQuestionOption {
|
|
31
|
+
/** 1-40 letters, digits, `-` or `_`; unique within its question. */
|
|
32
|
+
id: string;
|
|
33
|
+
/** 1-75 characters, the Slack button limit. */
|
|
34
|
+
label: string;
|
|
35
|
+
/** At most one option per question; omitted means false. */
|
|
36
|
+
recommended?: true;
|
|
37
|
+
}
|
|
38
|
+
interface GateQuestion {
|
|
39
|
+
/** 1-40 letters, digits, `-` or `_`; unique within the gate. */
|
|
40
|
+
id: string;
|
|
41
|
+
/** 1-500 characters. */
|
|
42
|
+
question: string;
|
|
43
|
+
/** 2-4 options. */
|
|
44
|
+
options: GateQuestionOption[];
|
|
45
|
+
}
|
|
46
|
+
/** What the owner reads instead of the machine-facing prompt. A store with `requireBrief` refuses a gate without one. */
|
|
47
|
+
interface GateBrief {
|
|
48
|
+
/** 1-280 characters on one line. */
|
|
49
|
+
summary: string;
|
|
50
|
+
/** 1-500 characters on one line. */
|
|
51
|
+
evidenceRef: string;
|
|
52
|
+
/** 1-4 questions; absent means the gate's schema alone defines the answer. */
|
|
53
|
+
questions?: GateQuestion[];
|
|
23
54
|
}
|
|
24
55
|
/** Binds a gate to the authority rule that opened it, so only that rule's resolver classes may answer. */
|
|
25
56
|
interface GateRule {
|
|
@@ -47,6 +78,17 @@ type GateAuthorization = {
|
|
|
47
78
|
* never admit one the default refused. Must return synchronously.
|
|
48
79
|
*/
|
|
49
80
|
type GateAuthorize = (gate: Readonly<GateRecord>, resolver: Readonly<GateResolver>) => GateAuthorization;
|
|
81
|
+
/**
|
|
82
|
+
* One answer a non-owner class may give, declared by the product when it builds the store.
|
|
83
|
+
* It admits exactly: `resolverClass`, a gate whose step is `stepId` (a bare id or `<id>:<n>` after
|
|
84
|
+
* the last `/`), and a payload deep-equal to `payload`. Evaluated in place of the default refusal for
|
|
85
|
+
* those gates only; the gate's rule and `authorize` still run after it. The resolver's id must be non-blank.
|
|
86
|
+
*/
|
|
87
|
+
interface GateAnswerAllowance {
|
|
88
|
+
resolverClass: ActorClass;
|
|
89
|
+
stepId: string;
|
|
90
|
+
payload: Readonly<Record<string, unknown>>;
|
|
91
|
+
}
|
|
50
92
|
interface GateInput {
|
|
51
93
|
/** Defaults to a random UUID. Supply one to make the gate addressable by a name you already own. */
|
|
52
94
|
id?: string;
|
|
@@ -54,6 +96,10 @@ interface GateInput {
|
|
|
54
96
|
schema?: JsonSchema;
|
|
55
97
|
expiresAt?: Date | string;
|
|
56
98
|
rule?: GateRule;
|
|
99
|
+
/** Required, with `evidenceRef`, when the store has `requireBrief`. */
|
|
100
|
+
summary?: string;
|
|
101
|
+
evidenceRef?: string;
|
|
102
|
+
questions?: GateQuestion[];
|
|
57
103
|
}
|
|
58
104
|
/**
|
|
59
105
|
* Synchronous on purpose: both implementations are local (a Map, and
|
|
@@ -112,6 +158,10 @@ declare class GateRuleInvalid extends GateError {
|
|
|
112
158
|
readonly reason: string;
|
|
113
159
|
constructor(gateId: string, reason: string);
|
|
114
160
|
}
|
|
161
|
+
declare class GateBriefInvalid extends GateError {
|
|
162
|
+
readonly issues: string[];
|
|
163
|
+
constructor(gateId: string, issues: string[]);
|
|
164
|
+
}
|
|
115
165
|
declare class GateAuthorizeInvalid extends GateError {
|
|
116
166
|
constructor(gateId: string);
|
|
117
167
|
}
|
|
@@ -123,7 +173,9 @@ declare class GateAuthorizeInvalid extends GateError {
|
|
|
123
173
|
declare abstract class BaseGateStore implements GateStore {
|
|
124
174
|
protected readonly clock: () => number;
|
|
125
175
|
private readonly authorize?;
|
|
126
|
-
|
|
176
|
+
private readonly requireBrief;
|
|
177
|
+
protected constructor(clock: () => number, authorize?: GateAuthorize | undefined, requireBrief?: boolean, allowances?: readonly GateAnswerAllowance[]);
|
|
178
|
+
private readonly allowances;
|
|
127
179
|
protected abstract insert(record: GateRecord): void;
|
|
128
180
|
protected abstract read(id: string): GateRecord | undefined;
|
|
129
181
|
protected abstract update(record: GateRecord): void;
|
|
@@ -134,11 +186,11 @@ declare abstract class BaseGateStore implements GateStore {
|
|
|
134
186
|
cancel(id: string, reason: string): GateRecord;
|
|
135
187
|
listPending(): GateRecord[];
|
|
136
188
|
private requirePending;
|
|
137
|
-
/** The default check runs first, the gate's rule second and `authorize` last, so each can only narrow who may resolve. */
|
|
189
|
+
/** The default check (or a listed allowance) runs first, the gate's rule second and `authorize` last, so each can only narrow who may resolve. */
|
|
138
190
|
private requireAuthorized;
|
|
139
191
|
/** Expiry is lazy: nothing sweeps the table, so a read is what notices the deadline passed. */
|
|
140
192
|
private lapseIfExpired;
|
|
141
193
|
protected nowIso(): string;
|
|
142
194
|
}
|
|
143
195
|
|
|
144
|
-
export { BaseGateStore as B, type GateRecord as G, type JsonSchema as J, type
|
|
196
|
+
export { BaseGateStore as B, type GateRecord as G, type JsonSchema as J, type GateQuestion as a, type GateStore as b, type GateResolver as c, type GateAuthorize as d, type GateAnswerAllowance as e, GateAborted as f, GateAlreadyExists as g, GateAlreadySettled as h, type GateAuthorization as i, GateAuthorizeInvalid as j, type GateBrief as k, GateBriefInvalid as l, GateCancelled as m, GateError as n, GateExpired as o, type GateInput as p, GateNotFound as q, GatePayloadInvalid as r, type GateQuestionOption as s, GateResolverRefused as t, type GateRule as u, GateRuleInvalid as v, type GateStatus as w, GateStoreSchemaOutdated as x };
|
|
@@ -74,12 +74,107 @@ var GateRuleInvalid = class extends GateError {
|
|
|
74
74
|
}
|
|
75
75
|
reason;
|
|
76
76
|
};
|
|
77
|
+
var GateBriefInvalid = class extends GateError {
|
|
78
|
+
constructor(gateId, issues) {
|
|
79
|
+
super(`gate ${gateId} brief is invalid: ${issues.join("; ")}`, gateId);
|
|
80
|
+
this.issues = issues;
|
|
81
|
+
}
|
|
82
|
+
issues;
|
|
83
|
+
};
|
|
77
84
|
var GateAuthorizeInvalid = class extends GateError {
|
|
78
85
|
constructor(gateId) {
|
|
79
86
|
super(`authorize for gate ${gateId} must return a decision { allowed: boolean } synchronously`, gateId);
|
|
80
87
|
}
|
|
81
88
|
};
|
|
82
89
|
|
|
90
|
+
// src/gate-brief.ts
|
|
91
|
+
var MAX_SUMMARY = 280;
|
|
92
|
+
var MAX_EVIDENCE_REF = 500;
|
|
93
|
+
var MAX_QUESTIONS = 4;
|
|
94
|
+
var MIN_OPTIONS = 2;
|
|
95
|
+
var MAX_OPTIONS = 4;
|
|
96
|
+
var MAX_OPTION_LABEL = 75;
|
|
97
|
+
var MAX_QUESTION_TEXT = 500;
|
|
98
|
+
var ID_PATTERN = /^[A-Za-z0-9_-]{1,40}$/;
|
|
99
|
+
var ID_RULE = 'id must be 1-40 letters, digits, "-" or "_"';
|
|
100
|
+
var CONTROL_CHAR = /[\u0000-\u001f\u007f]/;
|
|
101
|
+
function snapshotBrief(gateId, raw, required) {
|
|
102
|
+
const { summary, evidenceRef, questions } = raw ?? {};
|
|
103
|
+
const issues = [
|
|
104
|
+
...lineIssues("summary", summary, MAX_SUMMARY, required),
|
|
105
|
+
...lineIssues("evidenceRef", evidenceRef, MAX_EVIDENCE_REF, required)
|
|
106
|
+
];
|
|
107
|
+
const questionIssue = questions === void 0 ? void 0 : questionsIssue(questions);
|
|
108
|
+
if (questionIssue) issues.push(questionIssue);
|
|
109
|
+
if (issues.length > 0) throw new GateBriefInvalid(gateId, issues);
|
|
110
|
+
return Object.freeze({
|
|
111
|
+
summary,
|
|
112
|
+
evidenceRef,
|
|
113
|
+
questions: questions === void 0 ? void 0 : copyQuestions(questions)
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function lineIssues(field, value, max, required) {
|
|
117
|
+
if (value === void 0) return required ? [`${field} is required`] : [];
|
|
118
|
+
if (typeof value !== "string") return [`${field} must be a string`];
|
|
119
|
+
if (value.trim() === "") return [`${field} is blank`];
|
|
120
|
+
if (value.length > max) return [`${field} is over ${max} characters`];
|
|
121
|
+
if (CONTROL_CHAR.test(value)) return [`${field} must be one line with no control characters`];
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
function questionsIssue(value) {
|
|
125
|
+
if (!Array.isArray(value)) return "questions must be an array";
|
|
126
|
+
if (value.length < 1 || value.length > MAX_QUESTIONS) return `ask 1 to ${MAX_QUESTIONS} questions (got ${value.length})`;
|
|
127
|
+
const ids = /* @__PURE__ */ new Set();
|
|
128
|
+
for (const [index, raw] of value.entries()) {
|
|
129
|
+
const question = raw ?? {};
|
|
130
|
+
const where = `question ${index + 1}`;
|
|
131
|
+
if (typeof question.id !== "string" || !ID_PATTERN.test(question.id)) return `${where}: ${ID_RULE}`;
|
|
132
|
+
if (ids.has(question.id)) return `${where}: duplicate question id "${question.id}"`;
|
|
133
|
+
ids.add(question.id);
|
|
134
|
+
const issue = questionTextIssue(question.question) ?? optionsIssue(question.options);
|
|
135
|
+
if (issue) return `${where}: ${issue}`;
|
|
136
|
+
}
|
|
137
|
+
return void 0;
|
|
138
|
+
}
|
|
139
|
+
function questionTextIssue(text) {
|
|
140
|
+
if (typeof text !== "string" || text.trim() === "") return "question text is required";
|
|
141
|
+
if (text.length > MAX_QUESTION_TEXT) return `question text is over ${MAX_QUESTION_TEXT} characters`;
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
function optionsIssue(options) {
|
|
145
|
+
if (!Array.isArray(options) || options.length < MIN_OPTIONS || options.length > MAX_OPTIONS) {
|
|
146
|
+
return `give ${MIN_OPTIONS} to ${MAX_OPTIONS} options`;
|
|
147
|
+
}
|
|
148
|
+
const ids = /* @__PURE__ */ new Set();
|
|
149
|
+
let recommended = 0;
|
|
150
|
+
for (const [index, raw] of options.entries()) {
|
|
151
|
+
const option = raw ?? {};
|
|
152
|
+
const issue = optionIssue(option, ids);
|
|
153
|
+
if (issue) return `option ${index + 1}: ${issue}`;
|
|
154
|
+
ids.add(option.id);
|
|
155
|
+
if (option.recommended === true) recommended++;
|
|
156
|
+
}
|
|
157
|
+
return recommended > 1 ? "mark at most one option as recommended" : void 0;
|
|
158
|
+
}
|
|
159
|
+
function optionIssue(option, seen) {
|
|
160
|
+
if (typeof option.id !== "string" || !ID_PATTERN.test(option.id)) return ID_RULE;
|
|
161
|
+
if (seen.has(option.id)) return `duplicate option id "${option.id}"`;
|
|
162
|
+
if (typeof option.label !== "string" || option.label.trim() === "") return "label is required";
|
|
163
|
+
if (option.label.length > MAX_OPTION_LABEL) return `label is over ${MAX_OPTION_LABEL} characters`;
|
|
164
|
+
if (option.recommended !== void 0 && typeof option.recommended !== "boolean") return "recommended must be true or false";
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
167
|
+
function copyQuestions(questions) {
|
|
168
|
+
return questions.map((question) => ({
|
|
169
|
+
id: question.id,
|
|
170
|
+
question: question.question,
|
|
171
|
+
options: question.options.map(copyOption)
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
function copyOption(option) {
|
|
175
|
+
return option.recommended === true ? { id: option.id, label: option.label, recommended: true } : { id: option.id, label: option.label };
|
|
176
|
+
}
|
|
177
|
+
|
|
83
178
|
// src/json-schema.ts
|
|
84
179
|
function checkAgainstJsonSchema(schema, value, path = "$") {
|
|
85
180
|
const issues = [];
|
|
@@ -157,6 +252,35 @@ function defaultResolverRefusal(resolver) {
|
|
|
157
252
|
if (RESOLVER_CLASSES.includes(resolver.class)) return void 0;
|
|
158
253
|
return `actor class ${resolver.class} may not resolve a gate`;
|
|
159
254
|
}
|
|
255
|
+
function snapshotAllowances(raw) {
|
|
256
|
+
return Object.freeze(
|
|
257
|
+
(raw ?? []).map(
|
|
258
|
+
({ resolverClass, stepId, payload }) => Object.freeze({ resolverClass, stepId, payload: Object.freeze(structuredClone(payload)) })
|
|
259
|
+
)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
function resolverRefusal(gateId, resolver, payload, allowances) {
|
|
263
|
+
const refusal = defaultResolverRefusal(resolver);
|
|
264
|
+
if (refusal === void 0) return void 0;
|
|
265
|
+
if (!matchesAllowance(allowances, gateId, resolver, payload)) return refusal;
|
|
266
|
+
return resolver.id.trim() === "" ? `actor class ${resolver.class} must name itself to resolve this gate` : void 0;
|
|
267
|
+
}
|
|
268
|
+
function matchesAllowance(allowances, gateId, resolver, payload) {
|
|
269
|
+
return allowances.some((a) => a.resolverClass === resolver.class && stepOf(gateId) === a.stepId && jsonEqual(payload, a.payload));
|
|
270
|
+
}
|
|
271
|
+
function jsonEqual(a, b) {
|
|
272
|
+
if (a === b) return true;
|
|
273
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
274
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
275
|
+
const proto = Object.getPrototypeOf(a);
|
|
276
|
+
if (proto !== Object.getPrototypeOf(b) || proto !== Object.prototype && proto !== Array.prototype && proto !== null) return false;
|
|
277
|
+
const keys = Object.keys(a);
|
|
278
|
+
if (keys.length !== Object.keys(b).length) return false;
|
|
279
|
+
return keys.every((key) => Object.hasOwn(b, key) && jsonEqual(a[key], b[key]));
|
|
280
|
+
}
|
|
281
|
+
function stepOf(gateId) {
|
|
282
|
+
return gateId.slice(gateId.lastIndexOf("/") + 1).replace(/:\d+$/, "");
|
|
283
|
+
}
|
|
160
284
|
function ruleResolverRefusal(record, resolver) {
|
|
161
285
|
if (!record.rule) return void 0;
|
|
162
286
|
if (record.rule.resolvers.includes(resolver.class)) return void 0;
|
|
@@ -210,15 +334,20 @@ function isActorClass(value) {
|
|
|
210
334
|
|
|
211
335
|
// src/base-store.ts
|
|
212
336
|
var BaseGateStore = class {
|
|
213
|
-
constructor(clock, authorize) {
|
|
337
|
+
constructor(clock, authorize, requireBrief = false, allowances) {
|
|
214
338
|
this.clock = clock;
|
|
215
339
|
this.authorize = authorize;
|
|
340
|
+
this.requireBrief = requireBrief;
|
|
341
|
+
this.allowances = snapshotAllowances(allowances);
|
|
216
342
|
}
|
|
217
343
|
clock;
|
|
218
344
|
authorize;
|
|
345
|
+
requireBrief;
|
|
346
|
+
allowances;
|
|
219
347
|
create(input) {
|
|
220
348
|
const id = input.id ?? globalThis.crypto.randomUUID();
|
|
221
349
|
const rule = input.rule === void 0 ? void 0 : snapshotRule(id, input.rule);
|
|
350
|
+
const brief = snapshotBrief(id, input, this.requireBrief);
|
|
222
351
|
if (this.read(id)) throw new GateAlreadyExists(id);
|
|
223
352
|
const record = {
|
|
224
353
|
id,
|
|
@@ -231,7 +360,8 @@ var BaseGateStore = class {
|
|
|
231
360
|
resolvedAt: void 0,
|
|
232
361
|
expiresAt: toIso(input.expiresAt),
|
|
233
362
|
resolvedBy: void 0,
|
|
234
|
-
rule
|
|
363
|
+
rule,
|
|
364
|
+
...brief
|
|
235
365
|
};
|
|
236
366
|
this.insert(record);
|
|
237
367
|
return record;
|
|
@@ -244,7 +374,7 @@ var BaseGateStore = class {
|
|
|
244
374
|
if (!resolvedBy) throw new GateResolverRefused(id, void 0, "a resolver is required");
|
|
245
375
|
const resolver = snapshotResolver(id, resolvedBy);
|
|
246
376
|
const record = this.requirePending(id);
|
|
247
|
-
this.requireAuthorized(record, resolver);
|
|
377
|
+
this.requireAuthorized(record, resolver, payload);
|
|
248
378
|
if (record.schema) {
|
|
249
379
|
const issues = checkAgainstJsonSchema(record.schema, payload);
|
|
250
380
|
if (issues.length > 0) throw new GatePayloadInvalid(id, issues);
|
|
@@ -269,9 +399,9 @@ var BaseGateStore = class {
|
|
|
269
399
|
if (record.status !== "pending") throw new GateAlreadySettled(id, record.status);
|
|
270
400
|
return record;
|
|
271
401
|
}
|
|
272
|
-
/** The default check runs first, the gate's rule second and `authorize` last, so each can only narrow who may resolve. */
|
|
273
|
-
requireAuthorized(record, resolver) {
|
|
274
|
-
const refusal =
|
|
402
|
+
/** The default check (or a listed allowance) runs first, the gate's rule second and `authorize` last, so each can only narrow who may resolve. */
|
|
403
|
+
requireAuthorized(record, resolver, payload) {
|
|
404
|
+
const refusal = resolverRefusal(record.id, resolver, payload, this.allowances);
|
|
275
405
|
if (refusal) throw new GateResolverRefused(record.id, resolver.class, refusal);
|
|
276
406
|
const ruleRefusal = ruleResolverRefusal(record, resolver);
|
|
277
407
|
if (ruleRefusal) throw new GateResolverRefused(record.id, resolver.class, ruleRefusal);
|
|
@@ -308,11 +438,23 @@ export {
|
|
|
308
438
|
GateResolverRefused,
|
|
309
439
|
GateStoreSchemaOutdated,
|
|
310
440
|
GateRuleInvalid,
|
|
441
|
+
GateBriefInvalid,
|
|
311
442
|
GateAuthorizeInvalid,
|
|
443
|
+
snapshotBrief,
|
|
312
444
|
checkAgainstJsonSchema,
|
|
313
445
|
defaultResolverRefusal,
|
|
446
|
+
matchesAllowance,
|
|
314
447
|
ruleResolverRefusal,
|
|
315
448
|
snapshotResolver,
|
|
316
449
|
BaseGateStore
|
|
317
450
|
};
|
|
318
|
-
|
|
451
|
+
/*!
|
|
452
|
+
* The question bounds and `questionsIssue` are adapted from openrig,
|
|
453
|
+
* https://github.com/mvschwarz/openrig, packages/daemon/src/domain/human-questions.ts
|
|
454
|
+
* (parseHumanQuestions). Copyright 2026 Mike Schwarz, licensed under the Apache License 2.0,
|
|
455
|
+
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
456
|
+
* Changed here: the checks report through GateBriefInvalid instead of a result union, and
|
|
457
|
+
* `recommended` keeps only `true`.
|
|
458
|
+
* @license Apache-2.0
|
|
459
|
+
*/
|
|
460
|
+
//# sourceMappingURL=chunk-TE2NXWWK.js.map
|