@titan-design/hitl 0.3.1 → 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 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 store = new SqliteGateStore(openDatabase("~/.local/state/thing/gates.sqlite3"));
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",
@@ -51,7 +54,11 @@ Somewhere else entirely, in a CLI, an MCP tool, or a dashboard route:
51
54
  ```ts
52
55
  import { resolveGate, cancelGate } from "@titan-design/hitl";
53
56
 
54
- resolveGate(store, "deploy-approval", { approved: true });
57
+ resolveGate(store, "deploy-approval", { approved: true }, {
58
+ class: "owner-terminal",
59
+ id: "owner",
60
+ channel: "cli",
61
+ });
55
62
  ```
56
63
 
57
64
  After a restart, re-attach by id instead of re-opening:
@@ -89,7 +96,7 @@ both pass the same behaviour suite.
89
96
 
90
97
  `SqliteGateStore` installs a `hitl_gate` table through store-sqlite's
91
98
  `runMigrations` on construction. Pass `migrate: false` and put `gateMigration(n)`
92
- in the product's own migration list when hitl shares a database with domain
99
+ and `gateResolverMigration(m)` in the product's own migration list when hitl shares a database with domain
93
100
  tables. `table` renames the table so one database can host several gate spaces.
94
101
  Timestamps are ISO-8601 strings, the shape store-sqlite writes and any surface
95
102
  can send on as-is.
@@ -99,7 +106,7 @@ tell its waiter why.
99
106
 
100
107
  ## Who resolved it
101
108
 
102
- `resolve` and `resolveGate` take an optional third argument, a `GateResolver`:
109
+ `resolve` and `resolveGate` require a third argument, a `GateResolver`:
103
110
  `{ class, id, channel, confirmEvent? }`. `class` is an actor class from
104
111
  `@titan-design/authority`. The store records it as `resolvedBy`.
105
112
 
@@ -116,23 +123,145 @@ agent or automation never answers a gate. It throws `GateResolverRefused` and th
116
123
  pending. The store reads each declared resolver field once into a frozen copy, and checks and stores
117
124
  only that copy. A store's `authorize` option runs after the class check and can refuse more,
118
125
  never fewer. It must return `{ allowed }` synchronously, or the store throws
119
- `GateAuthorizeInvalid`. With `authorize` installed, a resolve that names no resolver is refused.
126
+ `GateAuthorizeInvalid`.
120
127
  Refusals name the gate id and the actor class, never the resolver's other fields.
121
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
+
122
142
  hitl records a claim about the resolver; it cannot prove one. Any process that can write
123
143
  the database can claim any class.
124
144
 
125
145
  On SQLite the resolver lives in a `resolved_by` column that `gateResolverMigration(n)` adds,
126
- along with a trigger that refuses any resolve naming no resolver. Add it to your own
127
- migration list after `gateMigration`. It is idempotent and does not backfill: gates resolved
128
- before it read back with `resolvedBy` undefined. Until the migration runs, a store resolves
129
- without a resolver as before and throws `GateStoreSchemaOutdated` when given one, rather
130
- than dropping it. `migrate: true` does not run it yet.
146
+ along with a trigger that refuses any resolve naming no resolver. `migrate: true` runs it as
147
+ version 2; with `migrate: false`, add it to your own migration list after `gateMigration`. It
148
+ is idempotent and does not backfill: gates resolved before it read back with `resolvedBy`
149
+ undefined. `SqliteGateStore` checks for the column when it is constructed and throws
150
+ `GateStoreSchemaOutdated` naming `gateResolverMigration` when it is missing, or naming
151
+ `gateMigration` when the table does not exist. That error carries an empty `gateId`,
152
+ because no gate is involved yet.
131
153
 
132
154
  After the migration, a writer built on hitl 0.2.x fails when it resolves: SQLite aborts the
133
155
  statement with a raw error whose message is `hitl: resolvedBy required`. The same trigger
134
156
  refuses a direct insert of a resolved row with no resolver. Cancels from an old writer still
135
- work. The fix is to upgrade that writer so it passes a resolver.
157
+ work. The fix is to upgrade that writer so it passes a resolver. A caller on this release
158
+ never reaches the trigger: every store refuses a resolve with no resolver first.
159
+
160
+ ## Upgrading to 0.4
161
+
162
+ 0.4 makes the resolver required. It is a breaking release; a `^0.3` range does not pick it up.
163
+
164
+ 1. Pass a `GateResolver` to every `store.resolve`, `resolveGate` and workflow
165
+ `runtime.signal` call. The compiler finds each one. Name the class honestly: a human at
166
+ a terminal is `owner-terminal`, a human on a phone is `owner-remote`. An agent or an
167
+ automation cannot resolve a gate, by design.
168
+ 2. Add `gateResolverMigration(n)` to your migration list, after `gateMigration`, with the
169
+ next free version in your own list. `migrate: true` stores run it for you as version 2.
170
+ 3. Expect `GateStoreSchemaOutdated` naming `gateResolverMigration` from the
171
+ `SqliteGateStore` constructor if step 2 is missing. The store no longer opens over such a
172
+ table.
173
+ 4. Upgrade every process that writes the gate table at once. A writer still on 0.2 fails
174
+ each resolve with SQLite's `hitl: resolvedBy required` once the migration has run.
175
+
176
+ A caller that bypasses the type and resolves with no resolver gets `GateResolverRefused`
177
+ with the reason `a resolver is required` from every store, memory or SQLite, and the gate
178
+ stays pending.
179
+
180
+ ## Rule-bound gates
181
+
182
+ A gate can carry the authority rule that opened it. Pass `rule` to `create`:
183
+ `{ table, version, ruleId, resolvers }`, where `resolvers` lists the resolver classes the
184
+ rule admits, as `evaluate` from `@titan-design/authority` returns them.
185
+
186
+ ```ts
187
+ store.create({
188
+ id: "release-approval",
189
+ prompt: "Publish the release?",
190
+ rule: { table: "F5", version: "1.0.0", ruleId: "REL-CO", resolvers: ["owner-terminal"] },
191
+ });
192
+ ```
193
+
194
+ The store reads the rule once into a frozen copy, so the caller cannot widen it after
195
+ `create`. A rule that is not a non-empty list of resolver classes throws `GateRuleInvalid`
196
+ and creates nothing. A rule-bound gate refuses a resolver whose class the rule does not
197
+ name, and refuses a resolve that names no resolver. Both throw `GateResolverRefused` and
198
+ leave the gate pending. The check runs after the default class check and before
199
+ `authorize`, so it only narrows. A gate without a rule behaves as before.
200
+
201
+ On SQLite the rule lives in a `rule` column that `gateRuleMigration(n)` adds, along with a
202
+ trigger that aborts any update resolving a rule-bound row by a class outside its rule, or
203
+ changing the rule, with `hitl: resolver outside the gate rule`. That stops a writer built on
204
+ hitl 0.3.x, which knows nothing of rules, from widening who may answer. The migration is
205
+ idempotent and does not backfill. `migrate: true` runs it as version 3. A store whose table
206
+ lacks the column throws `GateStoreSchemaOutdated` naming `gateRuleMigration` when handed a
207
+ rule, rather than dropping it.
208
+
209
+ The trigger needs `resolved_by` to know who answered. On a table that has run
210
+ `gateRuleMigration` but not `gateResolverMigration`, the trigger aborts every raw resolve of
211
+ a rule-bound row, and no store opens over the table. Run both migrations in either order;
212
+ the second one installs the class-aware trigger.
213
+
214
+ The insert guard fires before SQLite's conflict handling. A raw insert with no rule onto a
215
+ pending rule-bound id therefore aborts with `hitl: a pending rule-bound gate cannot be
216
+ replaced`, whether it is a plain duplicate, `ON CONFLICT DO UPDATE`, `ON CONFLICT DO NOTHING`
217
+ or `INSERT OR IGNORE`; a duplicate plain insert reports that message, not `UNIQUE`. The guard
218
+ covers only pending rows. A raw `REPLACE` of a cancelled or resolved rule-bound row succeeds
219
+ and can leave a pending rule-less row. That is outside the store API: `create` is a plain
220
+ insert and throws on an existing id, and no store method replaces a row.
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.
136
265
 
137
266
  ## Settling
138
267
 
@@ -1,4 +1,4 @@
1
- import { ActorClass } from '@titan-design/authority';
1
+ import { ResolverClass, ActorClass } from '@titan-design/authority';
2
2
 
3
3
  /** A gate is a row, never a promise: pending work survives the process that opened it. */
4
4
  type GateStatus = "pending" | "resolved" | "cancelled" | "expired";
@@ -18,6 +18,46 @@ interface GateRecord {
18
18
  expiresAt: string | undefined;
19
19
  /** Who resolved the gate, as claimed by the caller; unset for gates resolved before stores recorded it. */
20
20
  resolvedBy: GateResolver | undefined;
21
+ /** The authority rule that opened the gate; unset for a gate no rule governs. */
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[];
54
+ }
55
+ /** Binds a gate to the authority rule that opened it, so only that rule's resolver classes may answer. */
56
+ interface GateRule {
57
+ table: string;
58
+ version: string;
59
+ ruleId: string;
60
+ resolvers: ResolverClass[];
21
61
  }
22
62
  /** A claim about who answered a gate. hitl records it and checks its class; it cannot prove it. */
23
63
  interface GateResolver {
@@ -38,12 +78,28 @@ type GateAuthorization = {
38
78
  * never admit one the default refused. Must return synchronously.
39
79
  */
40
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
+ }
41
92
  interface GateInput {
42
93
  /** Defaults to a random UUID. Supply one to make the gate addressable by a name you already own. */
43
94
  id?: string;
44
95
  prompt: string;
45
96
  schema?: JsonSchema;
46
97
  expiresAt?: Date | string;
98
+ rule?: GateRule;
99
+ /** Required, with `evidenceRef`, when the store has `requireBrief`. */
100
+ summary?: string;
101
+ evidenceRef?: string;
102
+ questions?: GateQuestion[];
47
103
  }
48
104
  /**
49
105
  * Synchronous on purpose: both implementations are local (a Map, and
@@ -52,7 +108,7 @@ interface GateInput {
52
108
  interface GateStore {
53
109
  create(input: GateInput): GateRecord;
54
110
  get(id: string): GateRecord | undefined;
55
- resolve(id: string, payload: unknown, resolvedBy?: GateResolver): GateRecord;
111
+ resolve(id: string, payload: unknown, resolvedBy: GateResolver): GateRecord;
56
112
  cancel(id: string, reason: string): GateRecord;
57
113
  listPending(): GateRecord[];
58
114
  }
@@ -89,12 +145,23 @@ declare class GateResolverRefused extends GateError {
89
145
  readonly reason: string;
90
146
  constructor(gateId: string, actorClass: string | undefined, reason: string);
91
147
  }
92
- /** The store cannot record a resolver because its table predates the resolver migration. */
148
+ /**
149
+ * The store cannot record a resolver or a rule because its table predates the migration that adds the column.
150
+ * `gateId` is empty when the store refuses at construction, before any gate is named.
151
+ */
93
152
  declare class GateStoreSchemaOutdated extends GateError {
94
153
  readonly table: string;
95
154
  readonly migration: string;
96
155
  constructor(gateId: string, table: string, migration: string);
97
156
  }
157
+ declare class GateRuleInvalid extends GateError {
158
+ readonly reason: string;
159
+ constructor(gateId: string, reason: string);
160
+ }
161
+ declare class GateBriefInvalid extends GateError {
162
+ readonly issues: string[];
163
+ constructor(gateId: string, issues: string[]);
164
+ }
98
165
  declare class GateAuthorizeInvalid extends GateError {
99
166
  constructor(gateId: string);
100
167
  }
@@ -106,22 +173,24 @@ declare class GateAuthorizeInvalid extends GateError {
106
173
  declare abstract class BaseGateStore implements GateStore {
107
174
  protected readonly clock: () => number;
108
175
  private readonly authorize?;
109
- protected constructor(clock: () => number, authorize?: GateAuthorize | undefined);
176
+ private readonly requireBrief;
177
+ protected constructor(clock: () => number, authorize?: GateAuthorize | undefined, requireBrief?: boolean, allowances?: readonly GateAnswerAllowance[]);
178
+ private readonly allowances;
110
179
  protected abstract insert(record: GateRecord): void;
111
180
  protected abstract read(id: string): GateRecord | undefined;
112
181
  protected abstract update(record: GateRecord): void;
113
182
  protected abstract readByStatus(status: GateRecord["status"]): GateRecord[];
114
183
  create(input: GateInput): GateRecord;
115
184
  get(id: string): GateRecord | undefined;
116
- resolve(id: string, payload: unknown, resolvedBy?: GateResolver): GateRecord;
185
+ resolve(id: string, payload: unknown, resolvedBy: GateResolver): GateRecord;
117
186
  cancel(id: string, reason: string): GateRecord;
118
187
  listPending(): GateRecord[];
119
188
  private requirePending;
120
- /** The default check runs first and `authorize` second, so `authorize` 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. */
121
190
  private requireAuthorized;
122
191
  /** Expiry is lazy: nothing sweeps the table, so a read is what notices the deadline passed. */
123
192
  private lapseIfExpired;
124
193
  protected nowIso(): string;
125
194
  }
126
195
 
127
- export { BaseGateStore as B, type GateRecord as G, type JsonSchema as J, type GateStore as a, type GateResolver as b, type GateAuthorize as c, GateAborted as d, GateAlreadyExists as e, GateAlreadySettled as f, type GateAuthorization as g, GateAuthorizeInvalid as h, GateCancelled as i, GateError as j, GateExpired as k, type GateInput as l, GateNotFound as m, GatePayloadInvalid as n, GateResolverRefused as o, type GateStatus as p, GateStoreSchemaOutdated as q };
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 };