@agentic-kit/harness 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.
Files changed (87) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +176 -0
  3. package/blueprint/blueprint-defaults.d.ts +2 -0
  4. package/blueprint/blueprint-defaults.js +123 -0
  5. package/blueprint/blueprint-schema.d.ts +138 -0
  6. package/blueprint/blueprint-schema.js +86 -0
  7. package/blueprint/field-type.d.ts +25 -0
  8. package/blueprint/field-type.js +99 -0
  9. package/blueprint/internal-policies.d.ts +2 -0
  10. package/blueprint/internal-policies.js +14 -0
  11. package/blueprint/policy-provisioning.d.ts +79 -0
  12. package/blueprint/policy-provisioning.js +570 -0
  13. package/esm/blueprint/blueprint-defaults.d.ts +2 -0
  14. package/esm/blueprint/blueprint-defaults.js +120 -0
  15. package/esm/blueprint/blueprint-schema.d.ts +138 -0
  16. package/esm/blueprint/blueprint-schema.js +83 -0
  17. package/esm/blueprint/field-type.d.ts +25 -0
  18. package/esm/blueprint/field-type.js +94 -0
  19. package/esm/blueprint/internal-policies.d.ts +2 -0
  20. package/esm/blueprint/internal-policies.js +10 -0
  21. package/esm/blueprint/policy-provisioning.d.ts +79 -0
  22. package/esm/blueprint/policy-provisioning.js +553 -0
  23. package/esm/gating/confirm-gate.d.ts +46 -0
  24. package/esm/gating/confirm-gate.js +55 -0
  25. package/esm/gating/decline-guard.d.ts +7 -0
  26. package/esm/gating/decline-guard.js +48 -0
  27. package/esm/gating/preview.d.ts +37 -0
  28. package/esm/gating/preview.js +5 -0
  29. package/esm/gating/prompts.d.ts +8 -0
  30. package/esm/gating/prompts.js +175 -0
  31. package/esm/index.d.ts +20 -0
  32. package/esm/index.js +20 -0
  33. package/esm/skills/fetch.d.ts +31 -0
  34. package/esm/skills/fetch.js +84 -0
  35. package/esm/skills/frontmatter.d.ts +7 -0
  36. package/esm/skills/frontmatter.js +28 -0
  37. package/esm/skills/integrity.d.ts +8 -0
  38. package/esm/skills/integrity.js +28 -0
  39. package/esm/skills/manifest.d.ts +39 -0
  40. package/esm/skills/manifest.js +36 -0
  41. package/esm/skills/materialize.d.ts +13 -0
  42. package/esm/skills/materialize.js +26 -0
  43. package/esm/skills/overlay.d.ts +11 -0
  44. package/esm/skills/overlay.js +25 -0
  45. package/esm/skills/registry.d.ts +36 -0
  46. package/esm/skills/registry.js +30 -0
  47. package/esm/skills/source.d.ts +21 -0
  48. package/esm/skills/source.js +51 -0
  49. package/esm/skills/update-check.d.ts +36 -0
  50. package/esm/skills/update-check.js +74 -0
  51. package/esm/stash.d.ts +20 -0
  52. package/esm/stash.js +19 -0
  53. package/esm/types.d.ts +60 -0
  54. package/esm/types.js +8 -0
  55. package/gating/confirm-gate.d.ts +46 -0
  56. package/gating/confirm-gate.js +58 -0
  57. package/gating/decline-guard.d.ts +7 -0
  58. package/gating/decline-guard.js +52 -0
  59. package/gating/preview.d.ts +37 -0
  60. package/gating/preview.js +6 -0
  61. package/gating/prompts.d.ts +8 -0
  62. package/gating/prompts.js +179 -0
  63. package/index.d.ts +20 -0
  64. package/index.js +36 -0
  65. package/package.json +42 -0
  66. package/skills/fetch.d.ts +31 -0
  67. package/skills/fetch.js +122 -0
  68. package/skills/frontmatter.d.ts +7 -0
  69. package/skills/frontmatter.js +31 -0
  70. package/skills/integrity.d.ts +8 -0
  71. package/skills/integrity.js +64 -0
  72. package/skills/manifest.d.ts +39 -0
  73. package/skills/manifest.js +40 -0
  74. package/skills/materialize.d.ts +13 -0
  75. package/skills/materialize.js +62 -0
  76. package/skills/overlay.d.ts +11 -0
  77. package/skills/overlay.js +28 -0
  78. package/skills/registry.d.ts +36 -0
  79. package/skills/registry.js +34 -0
  80. package/skills/source.d.ts +21 -0
  81. package/skills/source.js +88 -0
  82. package/skills/update-check.d.ts +36 -0
  83. package/skills/update-check.js +110 -0
  84. package/stash.d.ts +20 -0
  85. package/stash.js +55 -0
  86. package/types.d.ts +60 -0
  87. package/types.js +9 -0
@@ -0,0 +1,553 @@
1
+ /**
2
+ * VENDORED — verbatim from @constructive-io/data (workspace-only, unpublished):
3
+ * constructive-client/packages/data/src/policy-provisioning.ts
4
+ *
5
+ * Zero-import pure-config module. Vendored because the package is not published
6
+ * to npm. Keep in sync with the upstream source. Do not edit by hand except to
7
+ * re-sync from upstream.
8
+ */
9
+ export const MEMBERSHIP_TYPES = {
10
+ APP: 1,
11
+ ORGANIZATION: 2,
12
+ GROUP: 3,
13
+ };
14
+ export const CRUD_OPERATIONS = ['create', 'read', 'update', 'delete'];
15
+ export const CRUD_TO_PRIVILEGE = {
16
+ create: 'INSERT',
17
+ read: 'SELECT',
18
+ update: 'UPDATE',
19
+ delete: 'DELETE',
20
+ };
21
+ export const DEFAULT_CRUD_GRANT_PRIVILEGES = [
22
+ ['select', '*'],
23
+ ['insert', '*'],
24
+ ['update', '*'],
25
+ ['delete', '*'],
26
+ ];
27
+ export const POLICY_PROVISIONING_CONFIG = {
28
+ AuthzDirectOwner: {
29
+ category: 'has-module',
30
+ hasDataNode: true,
31
+ dataNodeType: 'DataDirectOwner',
32
+ fieldOverrides: {
33
+ entity_field: {
34
+ type: 'string',
35
+ required: true,
36
+ label: 'Owner Field Name',
37
+ placeholder: 'owner_id',
38
+ description: 'Column that stores the owner user ID',
39
+ defaultValue: 'owner_id',
40
+ },
41
+ },
42
+ },
43
+ AuthzEntityMembership: {
44
+ category: 'has-module',
45
+ hasDataNode: true,
46
+ dataNodeType: 'DataEntityMembership',
47
+ fieldOverrides: {
48
+ entity_field: {
49
+ type: 'string',
50
+ required: true,
51
+ label: 'Entity Field Name',
52
+ placeholder: 'entity_id',
53
+ description: 'Column referencing the org/group',
54
+ defaultValue: 'entity_id',
55
+ },
56
+ membership_type: {
57
+ type: 'integer',
58
+ hidden: true,
59
+ defaultValue: MEMBERSHIP_TYPES.ORGANIZATION,
60
+ },
61
+ permission: {
62
+ type: 'string',
63
+ component: 'permission-select',
64
+ label: 'Required Permission',
65
+ description: 'Optional permission the user must have',
66
+ },
67
+ is_admin: {
68
+ type: 'boolean',
69
+ label: 'Admins',
70
+ description: 'People with admin access can view this.',
71
+ },
72
+ is_owner: {
73
+ type: 'boolean',
74
+ label: 'Owners',
75
+ description: 'People who own the membership can view this.',
76
+ },
77
+ },
78
+ },
79
+ AuthzDirectOwnerAny: {
80
+ category: 'needs-fields',
81
+ hasDataNode: false,
82
+ fieldOverrides: {
83
+ entity_fields: {
84
+ type: 'uuid[]',
85
+ required: true,
86
+ label: 'Owner Fields',
87
+ description: 'UUID columns to check for ownership (OR logic)',
88
+ defaultValue: ['owner_id'],
89
+ pgType: 'uuid',
90
+ },
91
+ },
92
+ },
93
+ AuthzAppMembership: {
94
+ category: 'no-fields',
95
+ hasDataNode: false,
96
+ fieldOverrides: {
97
+ permission: {
98
+ type: 'string',
99
+ component: 'permission-select',
100
+ label: 'Required Permission',
101
+ description: 'Optional permission the user must have',
102
+ },
103
+ is_admin: {
104
+ type: 'boolean',
105
+ label: 'Admins',
106
+ description: 'People with admin access can view this.',
107
+ },
108
+ is_owner: {
109
+ type: 'boolean',
110
+ label: 'Owners',
111
+ description: 'People who own the membership can view this.',
112
+ },
113
+ },
114
+ },
115
+ AuthzPublishable: {
116
+ category: 'has-module',
117
+ hasDataNode: true,
118
+ dataNodeType: 'DataPublishable',
119
+ fieldOverrides: {
120
+ is_published_field: {
121
+ type: 'string',
122
+ label: 'Published Flag Field',
123
+ placeholder: 'is_published',
124
+ defaultValue: 'is_published',
125
+ },
126
+ published_at_field: {
127
+ type: 'string',
128
+ label: 'Published At Field',
129
+ placeholder: 'published_at',
130
+ defaultValue: 'published_at',
131
+ },
132
+ require_published_at: {
133
+ type: 'boolean',
134
+ label: 'Require Published At',
135
+ description: 'Also require that the publish date has passed',
136
+ defaultValue: true,
137
+ },
138
+ },
139
+ },
140
+ AuthzTemporal: {
141
+ category: 'needs-fields',
142
+ hasDataNode: false,
143
+ fieldOverrides: {
144
+ valid_from_field: {
145
+ type: 'string',
146
+ required: true,
147
+ label: 'Valid From Field',
148
+ placeholder: 'valid_from',
149
+ description: 'Column for start time',
150
+ defaultValue: 'valid_from',
151
+ pgType: 'timestamptz',
152
+ },
153
+ valid_until_field: {
154
+ type: 'string',
155
+ required: true,
156
+ label: 'Valid Until Field',
157
+ placeholder: 'valid_until',
158
+ description: 'Column for end time',
159
+ defaultValue: 'valid_until',
160
+ pgType: 'timestamptz',
161
+ },
162
+ valid_from_inclusive: {
163
+ type: 'boolean',
164
+ label: 'Include Start',
165
+ description: 'Include start boundary',
166
+ defaultValue: true,
167
+ },
168
+ valid_until_inclusive: {
169
+ type: 'boolean',
170
+ label: 'Include End',
171
+ description: 'Include end boundary',
172
+ defaultValue: false,
173
+ },
174
+ },
175
+ },
176
+ AuthzMemberList: {
177
+ category: 'needs-fields',
178
+ hasDataNode: false,
179
+ fieldOverrides: {
180
+ array_field: {
181
+ type: 'string',
182
+ required: true,
183
+ label: 'Member List Field',
184
+ placeholder: 'allowed_users',
185
+ description: 'Column containing list of user IDs',
186
+ defaultValue: 'allowed_users',
187
+ pgType: 'uuid[]',
188
+ },
189
+ },
190
+ },
191
+ AuthzRelatedMemberList: {
192
+ category: 'needs-table',
193
+ hasDataNode: false,
194
+ fieldOverrides: {
195
+ owned_schema: {
196
+ hidden: true,
197
+ },
198
+ owned_table: {
199
+ type: 'string',
200
+ required: true,
201
+ component: 'table-select',
202
+ label: 'Related Table',
203
+ placeholder: 'groups',
204
+ description: 'Table containing the member list',
205
+ },
206
+ owned_table_key: {
207
+ type: 'uuid',
208
+ required: true,
209
+ label: 'Member List Column',
210
+ placeholder: 'member_ids',
211
+ description: 'List column in related table',
212
+ dependsOn: 'owned_table',
213
+ },
214
+ owned_table_ref_key: {
215
+ type: 'uuid',
216
+ required: true,
217
+ label: 'Reference Key',
218
+ placeholder: 'id',
219
+ description: 'FK column in related table',
220
+ dependsOn: 'owned_table',
221
+ },
222
+ this_object_key: {
223
+ type: 'uuid',
224
+ required: true,
225
+ label: 'Foreign Key',
226
+ placeholder: 'group_id',
227
+ description: 'FK on this table to related table',
228
+ },
229
+ },
230
+ },
231
+ AuthzRelatedEntityMembership: {
232
+ category: 'needs-table',
233
+ hasDataNode: false,
234
+ fieldOverrides: {
235
+ entity_field: {
236
+ type: 'uuid',
237
+ required: true,
238
+ label: 'Foreign Key Field',
239
+ description: 'Field on this table referencing the linked table',
240
+ },
241
+ membership_type: {
242
+ type: 'integer',
243
+ required: true,
244
+ component: 'membership-type-select',
245
+ label: 'Membership Scope',
246
+ },
247
+ obj_schema: {
248
+ hidden: true,
249
+ },
250
+ obj_table: {
251
+ type: 'string',
252
+ required: true,
253
+ component: 'table-select',
254
+ label: 'Linked Table',
255
+ description: 'Table to check for membership',
256
+ },
257
+ obj_field: {
258
+ type: 'uuid',
259
+ required: true,
260
+ label: 'Entity Field on Linked Table',
261
+ description: 'Field containing entity ID for membership check',
262
+ dependsOn: 'obj_table',
263
+ },
264
+ permission: {
265
+ type: 'string',
266
+ component: 'permission-select',
267
+ label: 'Required Permission',
268
+ },
269
+ is_admin: {
270
+ type: 'boolean',
271
+ label: 'Admins',
272
+ description: 'People with admin access can view this.',
273
+ },
274
+ is_owner: {
275
+ type: 'boolean',
276
+ label: 'Owners',
277
+ description: 'People who own the membership can view this.',
278
+ },
279
+ },
280
+ },
281
+ AuthzOrgHierarchy: {
282
+ category: 'has-module',
283
+ hasDataNode: true,
284
+ dataNodeType: 'DataOwnershipInEntity',
285
+ disabled: true,
286
+ fieldOverrides: {
287
+ direction: {
288
+ type: 'string',
289
+ required: true,
290
+ label: 'Direction',
291
+ description: 'Which direction to traverse the org hierarchy',
292
+ defaultValue: 'down',
293
+ options: [
294
+ { value: 'down', label: 'Down', description: 'Managers can see their subordinates' },
295
+ { value: 'up', label: 'Up', description: 'Subordinates can see their managers' },
296
+ ],
297
+ },
298
+ entity_field: {
299
+ type: 'string',
300
+ label: 'Entity Field Name',
301
+ placeholder: 'entity_id',
302
+ description: 'Field referencing the org entity',
303
+ defaultValue: 'entity_id',
304
+ },
305
+ anchor_field: {
306
+ type: 'string',
307
+ required: true,
308
+ label: 'Anchor Field',
309
+ placeholder: 'owner_id',
310
+ description: 'Field referencing the user (e.g., owner_id)',
311
+ defaultValue: 'owner_id',
312
+ },
313
+ max_depth: {
314
+ type: 'integer',
315
+ label: 'Max Depth',
316
+ description: 'Optional max depth to limit visibility',
317
+ },
318
+ },
319
+ },
320
+ AuthzAllowAll: {
321
+ category: 'no-fields',
322
+ hasDataNode: false,
323
+ },
324
+ AuthzDenyAll: {
325
+ category: 'no-fields',
326
+ hasDataNode: false,
327
+ },
328
+ AuthzComposite: {
329
+ category: 'no-fields',
330
+ hasDataNode: false,
331
+ },
332
+ };
333
+ export const DATA_NODE_GENERATED_FIELDS = {
334
+ DataDirectOwner: [
335
+ {
336
+ name: 'owner_id',
337
+ type: 'uuid',
338
+ nullable: false,
339
+ description: 'References the owning user',
340
+ },
341
+ ],
342
+ DataEntityMembership: [
343
+ {
344
+ name: 'entity_id',
345
+ type: 'uuid',
346
+ nullable: false,
347
+ description: 'References the organization or group',
348
+ },
349
+ ],
350
+ DataOwnershipInEntity: [
351
+ {
352
+ name: 'owner_id',
353
+ type: 'uuid',
354
+ nullable: false,
355
+ description: 'References the owning user',
356
+ },
357
+ {
358
+ name: 'entity_id',
359
+ type: 'uuid',
360
+ nullable: false,
361
+ description: 'References the organization or group',
362
+ },
363
+ ],
364
+ DataPublishable: [
365
+ {
366
+ name: 'is_published',
367
+ type: 'boolean',
368
+ nullable: false,
369
+ description: 'Whether the record is published',
370
+ },
371
+ {
372
+ name: 'published_at',
373
+ type: 'timestamptz',
374
+ nullable: true,
375
+ description: 'When the record was published',
376
+ },
377
+ ],
378
+ DataTimestamps: [
379
+ {
380
+ name: 'created_at',
381
+ type: 'timestamptz',
382
+ nullable: false,
383
+ description: 'When the record was created',
384
+ },
385
+ {
386
+ name: 'updated_at',
387
+ type: 'timestamptz',
388
+ nullable: false,
389
+ description: 'When the record was last updated',
390
+ },
391
+ ],
392
+ DataPeoplestamps: [
393
+ {
394
+ name: 'created_by',
395
+ type: 'uuid',
396
+ nullable: true,
397
+ description: 'User who created the record',
398
+ },
399
+ {
400
+ name: 'updated_by',
401
+ type: 'uuid',
402
+ nullable: true,
403
+ description: 'User who last updated the record',
404
+ },
405
+ ],
406
+ DataSoftDelete: [
407
+ {
408
+ name: 'is_deleted',
409
+ type: 'boolean',
410
+ nullable: false,
411
+ description: 'Whether the record is soft-deleted',
412
+ },
413
+ {
414
+ name: 'deleted_at',
415
+ type: 'timestamptz',
416
+ nullable: true,
417
+ description: 'When the record was deleted',
418
+ },
419
+ ],
420
+ };
421
+ const AUTO_INJECT_SCHEMA_FIELDS = new Set(['owned_schema', 'obj_schema']);
422
+ export function getPolicyProvisioningConfig(policyType) {
423
+ return POLICY_PROVISIONING_CONFIG[policyType];
424
+ }
425
+ export function getPolicyCategory(policyType) {
426
+ return POLICY_PROVISIONING_CONFIG[policyType]?.category ?? 'no-fields';
427
+ }
428
+ export function getDataNodeForPolicy(policyType) {
429
+ return POLICY_PROVISIONING_CONFIG[policyType]?.dataNodeType;
430
+ }
431
+ export function policyRequiresDataNode(policyType) {
432
+ return POLICY_PROVISIONING_CONFIG[policyType]?.hasDataNode === true;
433
+ }
434
+ export function policyCanBeNodeless(policyType) {
435
+ return POLICY_PROVISIONING_CONFIG[policyType]?.hasDataNode !== true;
436
+ }
437
+ export function getGeneratedFields(dataNodeType) {
438
+ return DATA_NODE_GENERATED_FIELDS[dataNodeType] ?? [];
439
+ }
440
+ export function hasGeneratedFields(dataNodeType) {
441
+ return dataNodeType in DATA_NODE_GENERATED_FIELDS;
442
+ }
443
+ export function getPolicyFieldDefaults(policyType) {
444
+ const config = POLICY_PROVISIONING_CONFIG[policyType];
445
+ if (!config?.fieldOverrides) {
446
+ return {};
447
+ }
448
+ const defaults = {};
449
+ for (const [key, override] of Object.entries(config.fieldOverrides)) {
450
+ if (override.defaultValue !== undefined) {
451
+ defaults[key] = override.defaultValue;
452
+ }
453
+ }
454
+ return defaults;
455
+ }
456
+ export function getFieldsRequiringColumns(policyType) {
457
+ const config = POLICY_PROVISIONING_CONFIG[policyType];
458
+ if (!config?.fieldOverrides) {
459
+ return [];
460
+ }
461
+ const result = [];
462
+ for (const [key, override] of Object.entries(config.fieldOverrides)) {
463
+ if (!override.pgType) {
464
+ continue;
465
+ }
466
+ const defaultName = Array.isArray(override.defaultValue)
467
+ ? override.defaultValue
468
+ : typeof override.defaultValue === 'string'
469
+ ? override.defaultValue
470
+ : key;
471
+ result.push({
472
+ key,
473
+ defaultName,
474
+ pgType: override.pgType,
475
+ });
476
+ }
477
+ return result;
478
+ }
479
+ export function sanitizePolicyData(data) {
480
+ const sanitized = {};
481
+ for (const [key, value] of Object.entries(data)) {
482
+ if (value === undefined || value === null) {
483
+ continue;
484
+ }
485
+ if (typeof value === 'string' && value.trim() === '') {
486
+ continue;
487
+ }
488
+ if (Array.isArray(value) && value.length === 0) {
489
+ continue;
490
+ }
491
+ sanitized[key] = value;
492
+ }
493
+ return sanitized;
494
+ }
495
+ export function injectSchemaFields(policyData, schemaId, policyType) {
496
+ const config = POLICY_PROVISIONING_CONFIG[policyType];
497
+ if (!config?.fieldOverrides) {
498
+ return policyData;
499
+ }
500
+ const next = { ...policyData };
501
+ for (const [key, override] of Object.entries(config.fieldOverrides)) {
502
+ if (!AUTO_INJECT_SCHEMA_FIELDS.has(key)) {
503
+ continue;
504
+ }
505
+ if (override.hidden && next[key] === undefined) {
506
+ next[key] = schemaId;
507
+ }
508
+ }
509
+ return next;
510
+ }
511
+ export function buildNodeDataForDataNodeType(dataNodeType, policyData) {
512
+ if (!dataNodeType) {
513
+ return {};
514
+ }
515
+ const data = {};
516
+ if (dataNodeType === 'DataDirectOwner') {
517
+ if (policyData.entity_field && policyData.entity_field !== 'owner_id') {
518
+ data.owner_field_name = policyData.entity_field;
519
+ }
520
+ }
521
+ if (dataNodeType === 'DataEntityMembership') {
522
+ if (policyData.entity_field && policyData.entity_field !== 'entity_id') {
523
+ data.entity_field_name = policyData.entity_field;
524
+ }
525
+ }
526
+ if (dataNodeType === 'DataOwnershipInEntity') {
527
+ if (policyData.entity_field && policyData.entity_field !== 'entity_id') {
528
+ data.entity_field_name = policyData.entity_field;
529
+ }
530
+ if (policyData.anchor_field && policyData.anchor_field !== 'owner_id') {
531
+ data.owner_field_name = policyData.anchor_field;
532
+ }
533
+ }
534
+ return data;
535
+ }
536
+ export function buildNodeData(policyType, policyData) {
537
+ return buildNodeDataForDataNodeType(getDataNodeForPolicy(policyType), policyData);
538
+ }
539
+ export function groupCrudOperationsByConfig(enabledOperations, operations, privilegeMap) {
540
+ const grouped = new Map();
541
+ for (const operation of enabledOperations) {
542
+ const config = operations[operation];
543
+ const key = `${config.roleName}:${config.isPermissive}`;
544
+ const current = grouped.get(key) ?? {
545
+ roleName: config.roleName,
546
+ isPermissive: config.isPermissive,
547
+ privileges: [],
548
+ };
549
+ current.privileges.push(privilegeMap[operation]);
550
+ grouped.set(key, current);
551
+ }
552
+ return [...grouped.values()];
553
+ }
@@ -0,0 +1,46 @@
1
+ import type { ConfirmPreview } from './preview';
2
+ /**
3
+ * Host-neutral confirm gate for mutating db tools. Structurally mirrors the
4
+ * pi extension `tool_call` hook so a pi adapter is a thin mapping, but takes
5
+ * every host capability (confirm UI, skip notification, context/token/preview
6
+ * resolvers) as injected deps — no Electron, no pi imports.
7
+ */
8
+ export type GateToolCallEvent = {
9
+ toolName: string;
10
+ toolCallId: string;
11
+ input?: Record<string, unknown>;
12
+ };
13
+ /** `block: true` stops the tool call; `reason` is surfaced to the agent. */
14
+ export type GateResult = {
15
+ block: true;
16
+ reason: string;
17
+ } | undefined;
18
+ export type GateHost = {
19
+ /** Whether a confirmation UI is available (headless hosts return false). */
20
+ hasUI: boolean;
21
+ /** Ask the user to approve the call. Resolve false to decline. */
22
+ confirmTool(toolCallId: string, title: string, message: string, preview?: ConfirmPreview): Promise<boolean>;
23
+ /** A retry of an already-declined call was auto-skipped without prompting. */
24
+ notifyToolSkipped(toolCallId: string): void;
25
+ };
26
+ export type ConfirmGateDeps = {
27
+ /**
28
+ * Whether the project is provisioned/runnable at `cwd`. Unrunnable calls
29
+ * skip the confirm so the tool can return its clean "provision first"
30
+ * message instead of making the user approve something that fails.
31
+ */
32
+ isProjectRunnable(cwd: string): Promise<boolean>;
33
+ /** Whether a data-plane token is available (gates `add_records`). */
34
+ hasDataToken(cwd: string): Promise<boolean>;
35
+ /**
36
+ * Resolve the preview for `create_template` (its tables live in the
37
+ * blueprint it copies, not in the tool input). Return undefined when a
38
+ * preview can't be built.
39
+ */
40
+ resolveTemplatePreview(cwd: string, blueprintName: string | undefined, displayName: string): Promise<ConfirmPreview | undefined>;
41
+ };
42
+ export type ConfirmGate = {
43
+ onAgentStart: () => void;
44
+ onToolCall: (event: GateToolCallEvent, host: GateHost, cwd: string) => Promise<GateResult>;
45
+ };
46
+ export declare function createConfirmGate(deps: ConfirmGateDeps): ConfirmGate;
@@ -0,0 +1,55 @@
1
+ import { buildDeclineReason, createDeclineGuard } from './decline-guard';
2
+ import { buildConfirmPrompt, MUTATING_DB_TOOLS } from './prompts';
3
+ export function createConfirmGate(deps) {
4
+ const declineGuard = createDeclineGuard();
5
+ async function confirmOrDecline(event, host, input, preview) {
6
+ const { title, message, preview: basePreview } = buildConfirmPrompt(event.toolName, input);
7
+ const approved = await host.confirmTool(event.toolCallId, title, message, preview ?? basePreview);
8
+ if (!approved) {
9
+ declineGuard.recordDecline(event.toolName, input);
10
+ return { block: true, reason: buildDeclineReason(event.toolName) };
11
+ }
12
+ // An approved mutation changes database state, so earlier declines may no
13
+ // longer describe the same effect (e.g. a create_template preview derives
14
+ // from the blueprint, not the input) — let them re-prompt with fresh eyes.
15
+ declineGuard.clear();
16
+ return undefined;
17
+ }
18
+ return {
19
+ onAgentStart: () => declineGuard.clear(),
20
+ onToolCall: async (event, host, cwd) => {
21
+ if (!MUTATING_DB_TOOLS.has(event.toolName))
22
+ return;
23
+ const input = event.input;
24
+ const retryBlock = declineGuard.checkRetry(event.toolName, input);
25
+ if (retryBlock) {
26
+ if (host.hasUI) {
27
+ host.notifyToolSkipped(event.toolCallId);
28
+ }
29
+ return { block: true, reason: retryBlock };
30
+ }
31
+ if (!host.hasUI) {
32
+ return {
33
+ block: true,
34
+ reason: `Cannot confirm "${event.toolName}" — no confirmation UI is available.`,
35
+ };
36
+ }
37
+ // provision_database is the tool that CREATES the project context, so it
38
+ // can't gate on an existing one — confirm it directly.
39
+ if (event.toolName === 'provision_database') {
40
+ return confirmOrDecline(event, host, input);
41
+ }
42
+ if (!(await deps.isProjectRunnable(cwd)))
43
+ return;
44
+ if (event.toolName === 'add_records' && !(await deps.hasDataToken(cwd)))
45
+ return;
46
+ let resolvedPreview;
47
+ if (event.toolName === 'create_template') {
48
+ const blueprintName = typeof input?.blueprintName === 'string' ? input.blueprintName : undefined;
49
+ const displayName = typeof input?.displayName === 'string' ? input.displayName : '';
50
+ resolvedPreview = await deps.resolveTemplatePreview(cwd, blueprintName, displayName);
51
+ }
52
+ return confirmOrDecline(event, host, input, resolvedPreview);
53
+ },
54
+ };
55
+ }
@@ -0,0 +1,7 @@
1
+ export type DeclineGuard = {
2
+ recordDecline: (toolName: string, input: Record<string, unknown> | undefined) => void;
3
+ checkRetry: (toolName: string, input: Record<string, unknown> | undefined) => string | null;
4
+ clear: () => void;
5
+ };
6
+ export declare function buildDeclineReason(toolName: string): string;
7
+ export declare function createDeclineGuard(): DeclineGuard;