@lightdash-tools/common 0.5.0 → 0.7.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 (94) hide show
  1. package/dist/agentops/extract-yaml-project.d.ts +7 -0
  2. package/dist/agentops/extract-yaml-project.js +42 -0
  3. package/dist/agentops/formatters.d.ts +5 -0
  4. package/dist/agentops/formatters.js +47 -0
  5. package/dist/agentops/snapshots.d.ts +32 -0
  6. package/dist/agentops/snapshots.js +34 -0
  7. package/dist/agentops/types.d.ts +224 -0
  8. package/dist/agentops/types.js +430 -0
  9. package/dist/argument-validation.d.ts +25 -0
  10. package/dist/argument-validation.js +199 -0
  11. package/dist/argument-validation.test.d.ts +1 -0
  12. package/dist/argument-validation.test.js +183 -0
  13. package/dist/audit.d.ts +9 -2
  14. package/dist/audit.js +17 -2
  15. package/dist/audit.test.d.ts +1 -0
  16. package/dist/audit.test.js +73 -0
  17. package/dist/env.d.ts +8 -0
  18. package/dist/env.js +9 -1
  19. package/dist/index.d.ts +7 -1
  20. package/dist/index.js +14 -0
  21. package/dist/input-validation.d.ts +35 -0
  22. package/dist/input-validation.js +87 -0
  23. package/dist/input-validation.test.js +93 -0
  24. package/dist/operations/agent-safe.d.ts +7 -0
  25. package/dist/operations/agent-safe.js +16 -0
  26. package/dist/operations/ai-agents.d.ts +6 -0
  27. package/dist/operations/ai-agents.js +411 -0
  28. package/dist/operations/index.d.ts +5 -0
  29. package/dist/operations/index.js +16 -0
  30. package/dist/operations/parity.test.d.ts +1 -0
  31. package/dist/operations/parity.test.js +28 -0
  32. package/dist/operations/registry.d.ts +10 -0
  33. package/dist/operations/registry.js +35 -0
  34. package/dist/operations/registry.test.d.ts +1 -0
  35. package/dist/operations/registry.test.js +196 -0
  36. package/dist/operations/types.d.ts +69 -0
  37. package/dist/operations/types.js +70 -0
  38. package/dist/operations/types.test.d.ts +1 -0
  39. package/dist/operations/types.test.js +104 -0
  40. package/dist/operations/users.d.ts +6 -0
  41. package/dist/operations/users.js +60 -0
  42. package/dist/safety.d.ts +27 -2
  43. package/dist/safety.js +52 -5
  44. package/dist/safety.test.js +217 -4
  45. package/dist/types/generated/openapi-types.d.ts +35895 -10622
  46. package/dist/types/generated/openapi-types.js +0 -1
  47. package/dist/types/lightdash-api.d.ts +1 -1
  48. package/dist/types/v1/ai-agents.d.ts +112 -3
  49. package/dist/types/v1/charts.d.ts +5 -3
  50. package/dist/types/v1/dashboards.d.ts +13 -0
  51. package/dist/types/v1/index.d.ts +45 -8
  52. package/dist/types/v1/index.js +1 -1
  53. package/dist/types/v1/lightdash-api.d.ts +107 -16
  54. package/dist/types/v1/users.d.ts +4 -0
  55. package/dist/types/v2/content.d.ts +2 -0
  56. package/dist/types/v2/content.js +8 -0
  57. package/dist/types/v2/index.d.ts +11 -9
  58. package/dist/types/v2/index.js +1 -1
  59. package/package.json +16 -3
  60. package/src/agentops/extract-yaml-project.ts +42 -0
  61. package/src/agentops/formatters.ts +52 -0
  62. package/src/agentops/snapshots.ts +56 -0
  63. package/src/agentops/types.ts +599 -0
  64. package/src/argument-validation.test.ts +270 -0
  65. package/src/argument-validation.ts +240 -0
  66. package/src/audit.test.ts +87 -0
  67. package/src/audit.ts +21 -3
  68. package/src/env.ts +9 -0
  69. package/src/index.test.ts +1 -0
  70. package/src/index.ts +16 -1
  71. package/src/input-validation.test.ts +157 -1
  72. package/src/input-validation.ts +95 -0
  73. package/src/operations/agent-safe.ts +18 -0
  74. package/src/operations/ai-agents.ts +451 -0
  75. package/src/operations/index.ts +19 -0
  76. package/src/operations/parity.test.ts +35 -0
  77. package/src/operations/registry.test.ts +219 -0
  78. package/src/operations/registry.ts +44 -0
  79. package/src/operations/types.test.ts +148 -0
  80. package/src/operations/types.ts +168 -0
  81. package/src/operations/users.ts +67 -0
  82. package/src/safety.test.ts +276 -4
  83. package/src/safety.ts +72 -4
  84. package/src/types/generated/openapi-types.ts +37017 -11241
  85. package/src/types/lightdash-api.ts +39 -0
  86. package/src/types/v1/ai-agents.ts +133 -3
  87. package/src/types/v1/charts.ts +5 -3
  88. package/src/types/v1/dashboards.ts +19 -0
  89. package/src/types/v1/index.ts +51 -8
  90. package/src/types/v1/lightdash-api.ts +129 -20
  91. package/src/types/v1/users.ts +4 -0
  92. package/src/types/v2/content.ts +8 -0
  93. package/src/types/v2/index.ts +11 -9
  94. package/tsconfig.json +1 -0
@@ -1,17 +1,95 @@
1
1
  import { describe, it, expect, afterEach } from 'vitest';
2
+
2
3
  import {
3
4
  SafetyMode,
4
5
  isAllowed,
6
+ isOperationAllowed,
5
7
  getSafetyModeFromEnv,
6
8
  getAllowedProjectUuidsFromEnv,
7
9
  isProjectAllowed,
8
10
  areAllProjectsAllowed,
11
+ extractProjectUuids,
9
12
  READ_ONLY_DEFAULT,
10
13
  WRITE_IDEMPOTENT,
14
+ WRITE_NONDESTRUCTIVE,
15
+ WRITE_OPEN_WORLD,
11
16
  WRITE_DESTRUCTIVE,
17
+ type OperationPolicy,
18
+ type SafetyImpact,
12
19
  } from './safety';
13
20
 
21
+ const UUID_A = '11111111-1111-1111-1111-111111111111';
22
+ const UUID_B = '22222222-2222-2222-2222-222222222222';
23
+ const UUID_C = '33333333-3333-3333-3333-333333333333';
24
+
25
+ const ALL_IMPACTS: SafetyImpact[] = [
26
+ 'read',
27
+ 'write-nondestructive',
28
+ 'write-destructive',
29
+ 'credential-sensitive',
30
+ 'external-side-effect',
31
+ ];
32
+
33
+ function policy(impact: SafetyImpact): OperationPolicy {
34
+ return { impact };
35
+ }
36
+
14
37
  describe('Safety Logic', () => {
38
+ describe('annotation presets', () => {
39
+ it('should define READ_ONLY_DEFAULT as read-only closed-world', () => {
40
+ expect(READ_ONLY_DEFAULT).toEqual({
41
+ readOnlyHint: true,
42
+ openWorldHint: false,
43
+ destructiveHint: false,
44
+ idempotentHint: true,
45
+ });
46
+ });
47
+
48
+ it('should define WRITE_IDEMPOTENT as deprecated non-destructive idempotent write', () => {
49
+ expect(WRITE_IDEMPOTENT).toEqual({
50
+ readOnlyHint: false,
51
+ openWorldHint: false,
52
+ destructiveHint: false,
53
+ idempotentHint: true,
54
+ });
55
+ });
56
+
57
+ it('should define WRITE_NONDESTRUCTIVE as non-destructive closed-world write', () => {
58
+ expect(WRITE_NONDESTRUCTIVE).toEqual({
59
+ readOnlyHint: false,
60
+ openWorldHint: false,
61
+ destructiveHint: false,
62
+ idempotentHint: false,
63
+ });
64
+ });
65
+
66
+ it('should define WRITE_OPEN_WORLD for LLM generation style tools', () => {
67
+ expect(WRITE_OPEN_WORLD).toEqual({
68
+ readOnlyHint: false,
69
+ openWorldHint: true,
70
+ destructiveHint: false,
71
+ idempotentHint: false,
72
+ });
73
+ });
74
+
75
+ it('should define WRITE_DESTRUCTIVE as destructive write', () => {
76
+ expect(WRITE_DESTRUCTIVE).toEqual({
77
+ readOnlyHint: false,
78
+ openWorldHint: false,
79
+ destructiveHint: true,
80
+ idempotentHint: false,
81
+ });
82
+ });
83
+ });
84
+
85
+ describe('SafetyMode enum', () => {
86
+ it('should keep WRITE_IDEMPOTENT as deprecated alias with distinct string value', () => {
87
+ expect(SafetyMode.WRITE_IDEMPOTENT).toBe('write-idempotent');
88
+ expect(SafetyMode.WRITE_NONDESTRUCTIVE).toBe('write-nondestructive');
89
+ expect(SafetyMode.WRITE_IDEMPOTENT).not.toBe(SafetyMode.WRITE_NONDESTRUCTIVE);
90
+ });
91
+ });
92
+
15
93
  describe('isAllowed', () => {
16
94
  it('should allow read-only operations in read-only mode', () => {
17
95
  expect(isAllowed(SafetyMode.READ_ONLY, READ_ONLY_DEFAULT)).toBe(true);
@@ -19,23 +97,86 @@ describe('Safety Logic', () => {
19
97
 
20
98
  it('should forbid non-read-only operations in read-only mode', () => {
21
99
  expect(isAllowed(SafetyMode.READ_ONLY, WRITE_IDEMPOTENT)).toBe(false);
100
+ expect(isAllowed(SafetyMode.READ_ONLY, WRITE_NONDESTRUCTIVE)).toBe(false);
101
+ expect(isAllowed(SafetyMode.READ_ONLY, WRITE_OPEN_WORLD)).toBe(false);
22
102
  expect(isAllowed(SafetyMode.READ_ONLY, WRITE_DESTRUCTIVE)).toBe(false);
23
103
  });
24
104
 
25
- it('should allow read-only and idempotent-write in write-idempotent mode', () => {
105
+ it('should allow read-only and non-destructive writes in write-idempotent mode', () => {
26
106
  expect(isAllowed(SafetyMode.WRITE_IDEMPOTENT, READ_ONLY_DEFAULT)).toBe(true);
27
107
  expect(isAllowed(SafetyMode.WRITE_IDEMPOTENT, WRITE_IDEMPOTENT)).toBe(true);
108
+ expect(isAllowed(SafetyMode.WRITE_IDEMPOTENT, WRITE_NONDESTRUCTIVE)).toBe(true);
109
+ expect(isAllowed(SafetyMode.WRITE_IDEMPOTENT, WRITE_OPEN_WORLD)).toBe(true);
110
+ });
111
+
112
+ it('should allow read-only and non-destructive writes in write-nondestructive mode', () => {
113
+ expect(isAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, READ_ONLY_DEFAULT)).toBe(true);
114
+ expect(isAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, WRITE_NONDESTRUCTIVE)).toBe(true);
115
+ expect(isAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, WRITE_OPEN_WORLD)).toBe(true);
28
116
  });
29
117
 
30
- it('should forbid destructive operations in write-idempotent mode', () => {
118
+ it('should forbid destructive operations in write-idempotent and write-nondestructive modes', () => {
31
119
  expect(isAllowed(SafetyMode.WRITE_IDEMPOTENT, WRITE_DESTRUCTIVE)).toBe(false);
120
+ expect(isAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, WRITE_DESTRUCTIVE)).toBe(false);
32
121
  });
33
122
 
34
123
  it('should allow all operations in write-destructive mode', () => {
35
124
  expect(isAllowed(SafetyMode.WRITE_DESTRUCTIVE, READ_ONLY_DEFAULT)).toBe(true);
36
125
  expect(isAllowed(SafetyMode.WRITE_DESTRUCTIVE, WRITE_IDEMPOTENT)).toBe(true);
126
+ expect(isAllowed(SafetyMode.WRITE_DESTRUCTIVE, WRITE_NONDESTRUCTIVE)).toBe(true);
127
+ expect(isAllowed(SafetyMode.WRITE_DESTRUCTIVE, WRITE_OPEN_WORLD)).toBe(true);
37
128
  expect(isAllowed(SafetyMode.WRITE_DESTRUCTIVE, WRITE_DESTRUCTIVE)).toBe(true);
38
129
  });
130
+
131
+ it('should default to permissive for an unknown mode string (backward compat)', () => {
132
+ expect(isAllowed('custom-mode', WRITE_DESTRUCTIVE)).toBe(true);
133
+ });
134
+ });
135
+
136
+ describe('isOperationAllowed', () => {
137
+ it('should allow only read impact in read-only mode', () => {
138
+ expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('read'))).toBe(true);
139
+ expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('write-nondestructive'))).toBe(false);
140
+ expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('write-destructive'))).toBe(false);
141
+ expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('credential-sensitive'))).toBe(false);
142
+ expect(isOperationAllowed(SafetyMode.READ_ONLY, policy('external-side-effect'))).toBe(false);
143
+ });
144
+
145
+ it('should allow read, write-nondestructive, and external-side-effect in write-nondestructive mode', () => {
146
+ expect(isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('read'))).toBe(true);
147
+ expect(
148
+ isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-nondestructive')),
149
+ ).toBe(true);
150
+ expect(
151
+ isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('external-side-effect')),
152
+ ).toBe(true);
153
+ expect(isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-destructive'))).toBe(
154
+ false,
155
+ );
156
+ expect(
157
+ isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy('credential-sensitive')),
158
+ ).toBe(false);
159
+ });
160
+
161
+ it('should treat write-idempotent mode the same as write-nondestructive for policy checks', () => {
162
+ for (const impact of ALL_IMPACTS) {
163
+ expect(isOperationAllowed(SafetyMode.WRITE_IDEMPOTENT, policy(impact))).toBe(
164
+ isOperationAllowed(SafetyMode.WRITE_NONDESTRUCTIVE, policy(impact)),
165
+ );
166
+ }
167
+ });
168
+
169
+ it('should allow all impacts in write-destructive mode', () => {
170
+ for (const impact of ALL_IMPACTS) {
171
+ expect(isOperationAllowed(SafetyMode.WRITE_DESTRUCTIVE, policy(impact))).toBe(true);
172
+ }
173
+ });
174
+
175
+ it('should fail closed for unknown mode strings', () => {
176
+ for (const impact of ALL_IMPACTS) {
177
+ expect(isOperationAllowed('custom-mode', policy(impact))).toBe(false);
178
+ }
179
+ });
39
180
  });
40
181
 
41
182
  describe('getSafetyModeFromEnv', () => {
@@ -50,9 +191,19 @@ describe('Safety Logic', () => {
50
191
  expect(getSafetyModeFromEnv()).toBe(SafetyMode.READ_ONLY);
51
192
  });
52
193
 
53
- it('should return value from env when set to valid mode', () => {
194
+ it('should return WRITE_NONDESTRUCTIVE when env is set to write-nondestructive', () => {
195
+ process.env.LIGHTDASH_TOOLS_SAFETY_MODE = SafetyMode.WRITE_NONDESTRUCTIVE;
196
+ expect(getSafetyModeFromEnv()).toBe(SafetyMode.WRITE_NONDESTRUCTIVE);
197
+ });
198
+
199
+ it('should map deprecated write-idempotent env value to WRITE_NONDESTRUCTIVE', () => {
54
200
  process.env.LIGHTDASH_TOOLS_SAFETY_MODE = SafetyMode.WRITE_IDEMPOTENT;
55
- expect(getSafetyModeFromEnv()).toBe(SafetyMode.WRITE_IDEMPOTENT);
201
+ expect(getSafetyModeFromEnv()).toBe(SafetyMode.WRITE_NONDESTRUCTIVE);
202
+ });
203
+
204
+ it('should return WRITE_DESTRUCTIVE when env is set to write-destructive', () => {
205
+ process.env.LIGHTDASH_TOOLS_SAFETY_MODE = SafetyMode.WRITE_DESTRUCTIVE;
206
+ expect(getSafetyModeFromEnv()).toBe(SafetyMode.WRITE_DESTRUCTIVE);
56
207
  });
57
208
 
58
209
  it('should return READ_ONLY when env is set to invalid value', () => {
@@ -129,4 +280,125 @@ describe('Safety Logic', () => {
129
280
  expect(areAllProjectsAllowed(['uuid-a'], ['uuid-b', 'uuid-c'])).toBe(false);
130
281
  });
131
282
  });
283
+
284
+ describe('extractProjectUuids', () => {
285
+ it('should return empty array for null', () => {
286
+ expect(extractProjectUuids(null)).toEqual([]);
287
+ });
288
+
289
+ it('should return empty array for undefined', () => {
290
+ expect(extractProjectUuids(undefined)).toEqual([]);
291
+ });
292
+
293
+ it('should extract projectUuid from MCP-style object', () => {
294
+ expect(extractProjectUuids({ projectUuid: UUID_A })).toEqual([UUID_A]);
295
+ });
296
+
297
+ it('should extract projectUuids array from MCP-style object', () => {
298
+ expect(extractProjectUuids({ projectUuids: [UUID_A, UUID_B] })).toEqual([UUID_A, UUID_B]);
299
+ });
300
+
301
+ it('should extract projects array from MCP-style object', () => {
302
+ expect(extractProjectUuids({ projects: [UUID_A, UUID_B] })).toEqual([UUID_A, UUID_B]);
303
+ });
304
+
305
+ it('should extract project from MCP-style object', () => {
306
+ expect(extractProjectUuids({ project: UUID_A })).toEqual([UUID_A]);
307
+ });
308
+
309
+ it('should extract UUID from CLI positional argument', () => {
310
+ expect(extractProjectUuids([UUID_A])).toEqual([UUID_A]);
311
+ });
312
+
313
+ it('should ignore non-UUID CLI positional strings', () => {
314
+ expect(extractProjectUuids(['not-a-uuid', 'also-invalid'])).toEqual([]);
315
+ });
316
+
317
+ it('should extract from nested CLI options with projects key', () => {
318
+ expect(extractProjectUuids([{ projects: [UUID_A, UUID_B] }])).toEqual([UUID_A, UUID_B]);
319
+ });
320
+
321
+ it('should extract from nested CLI options with project key', () => {
322
+ expect(extractProjectUuids([{ project: UUID_A }])).toEqual([UUID_A]);
323
+ });
324
+
325
+ it('should extract from nested CLI options with projectUuid key', () => {
326
+ expect(extractProjectUuids([{ projectUuid: UUID_A }])).toEqual([UUID_A]);
327
+ });
328
+
329
+ it('should extract from nested CLI options with projectUuids key', () => {
330
+ expect(extractProjectUuids([{ projectUuids: [UUID_A, UUID_B] }])).toEqual([UUID_A, UUID_B]);
331
+ });
332
+
333
+ it('should combine positional UUIDs and nested option values', () => {
334
+ expect(extractProjectUuids([UUID_A, { project: UUID_B }])).toEqual([UUID_A, UUID_B]);
335
+ });
336
+
337
+ it('should deduplicate UUIDs across sources', () => {
338
+ expect(
339
+ extractProjectUuids([
340
+ UUID_A,
341
+ { projectUuid: UUID_A, projectUuids: [UUID_B, UUID_C] },
342
+ { projects: [UUID_B, UUID_C] },
343
+ ]),
344
+ ).toEqual([UUID_A, UUID_B, UUID_C]);
345
+ });
346
+
347
+ it('should filter out empty strings from extracted values', () => {
348
+ expect(
349
+ extractProjectUuids({
350
+ projectUuid: '',
351
+ projectUuids: [UUID_A, '', UUID_B],
352
+ }),
353
+ ).toEqual([UUID_A, UUID_B]);
354
+ });
355
+
356
+ it('should filter empty strings from nested CLI options', () => {
357
+ expect(extractProjectUuids([{ projects: ['', UUID_A, ''] }])).toEqual([UUID_A]);
358
+ });
359
+
360
+ it('should return empty array for MCP object with no project keys', () => {
361
+ expect(extractProjectUuids({ query: 'test', limit: 10 })).toEqual([]);
362
+ });
363
+
364
+ it('should extract projectUuid from bundleYaml in MCP tool args', () => {
365
+ const projectUuid = '550e8400-e29b-41d4-a716-446655440000';
366
+ const bundleYaml = [
367
+ 'apiVersion: lightdash.ai/v1alpha1',
368
+ 'kind: LightdashAiAgentBundle',
369
+ 'metadata:',
370
+ ' name: test-bundle',
371
+ 'spec:',
372
+ ` projectUuid: ${projectUuid}`,
373
+ ' agents:',
374
+ ' - key: a1',
375
+ ' name: Agent One',
376
+ ' evaluations: []',
377
+ ].join('\n');
378
+ expect(extractProjectUuids({ bundleYaml })).toEqual([projectUuid]);
379
+ });
380
+
381
+ it('should extract projectUuid from gateYaml in MCP tool args', () => {
382
+ const projectUuid = '550e8400-e29b-41d4-a716-446655440000';
383
+ const agentUuid = '660e8400-e29b-41d4-a716-446655440001';
384
+ const evaluationUuid = '770e8400-e29b-41d4-a716-446655440002';
385
+ const gateYaml = [
386
+ 'apiVersion: lightdash.ai/v1alpha1',
387
+ 'kind: LightdashAiEvaluationGate',
388
+ 'metadata:',
389
+ ' name: release-gate',
390
+ 'spec:',
391
+ ` projectUuid: ${projectUuid}`,
392
+ ` agentUuid: ${agentUuid}`,
393
+ ` evaluationUuid: ${evaluationUuid}`,
394
+ ' policy:',
395
+ ' minPassRate: 0.9',
396
+ ].join('\n');
397
+ expect(extractProjectUuids({ gateYaml })).toEqual([projectUuid]);
398
+ });
399
+
400
+ it('should return empty array for empty CLI argument array', () => {
401
+ expect(extractProjectUuids([])).toEqual([]);
402
+ });
403
+ });
132
404
  });
package/src/safety.ts CHANGED
@@ -1,11 +1,25 @@
1
+ import { extractProjectUuidsFromToolArgs } from './agentops/extract-yaml-project';
1
2
  import { ENV_LIGHTDASH_TOOLS_SAFETY_MODE, ENV_LIGHTDASH_TOOLS_ALLOWED_PROJECTS } from './env';
2
3
 
4
+ /**
5
+ * Semantic impact classification for operations (RFC Phase 0).
6
+ * Used by {@link OperationPolicy} and {@link isOperationAllowed}.
7
+ */
8
+ export type SafetyImpact =
9
+ | 'credential-sensitive'
10
+ | 'external-side-effect'
11
+ | 'read'
12
+ | 'write-destructive'
13
+ | 'write-nondestructive';
14
+
3
15
  /**
4
16
  * Hierarchical safety modes for Lightdash AI tools and CLI.
5
17
  */
6
18
  export enum SafetyMode {
7
19
  READ_ONLY = 'read-only',
20
+ /** Deprecated alias — prefer {@link SafetyMode.WRITE_NONDESTRUCTIVE}. */
8
21
  WRITE_IDEMPOTENT = 'write-idempotent',
22
+ WRITE_NONDESTRUCTIVE = 'write-nondestructive',
9
23
  WRITE_DESTRUCTIVE = 'write-destructive',
10
24
  }
11
25
 
@@ -21,6 +35,11 @@ export type ToolAnnotations = {
21
35
  openWorldHint?: boolean;
22
36
  };
23
37
 
38
+ /** Policy describing the semantic impact of an operation. */
39
+ export type OperationPolicy = {
40
+ impact: SafetyImpact;
41
+ };
42
+
24
43
  /** Preset: read-only, non-destructive, idempotent, closed-world. Use for list/get/compile tools. */
25
44
  export const READ_ONLY_DEFAULT: ToolAnnotations = {
26
45
  readOnlyHint: true,
@@ -29,7 +48,10 @@ export const READ_ONLY_DEFAULT: ToolAnnotations = {
29
48
  idempotentHint: true,
30
49
  };
31
50
 
32
- /** Preset: write, non-destructive, idempotent (e.g. upsert by slug). Use for create/update tools. */
51
+ /**
52
+ * Legacy preset: write, non-destructive, idempotent (e.g. upsert by slug).
53
+ * Prefer {@link WRITE_NONDESTRUCTIVE} for new tools; idempotent behavior is not implied by safety mode.
54
+ */
33
55
  export const WRITE_IDEMPOTENT: ToolAnnotations = {
34
56
  readOnlyHint: false,
35
57
  openWorldHint: false,
@@ -37,6 +59,22 @@ export const WRITE_IDEMPOTENT: ToolAnnotations = {
37
59
  idempotentHint: true,
38
60
  };
39
61
 
62
+ /** Preset: write, non-destructive, closed-world. Use for create/update tools without idempotency guarantee. */
63
+ export const WRITE_NONDESTRUCTIVE: ToolAnnotations = {
64
+ readOnlyHint: false,
65
+ openWorldHint: false,
66
+ destructiveHint: false,
67
+ idempotentHint: false,
68
+ };
69
+
70
+ /** Preset: write, non-destructive, open-world. Use for LLM generation and similar external interactions. */
71
+ export const WRITE_OPEN_WORLD: ToolAnnotations = {
72
+ readOnlyHint: false,
73
+ openWorldHint: true,
74
+ destructiveHint: false,
75
+ idempotentHint: false,
76
+ };
77
+
40
78
  /** Preset: write, destructive, non-idempotent. Use for delete/remove tools. */
41
79
  export const WRITE_DESTRUCTIVE: ToolAnnotations = {
42
80
  readOnlyHint: false,
@@ -46,26 +84,54 @@ export const WRITE_DESTRUCTIVE: ToolAnnotations = {
46
84
  };
47
85
 
48
86
  /**
49
- * Validates if an operation is allowed in the current safety mode.
87
+ * Validates if an operation is allowed in the current safety mode using MCP tool annotations.
88
+ * Unknown modes default to permissive for backward compatibility.
50
89
  */
51
90
  export function isAllowed(mode: SafetyMode | string, annotations: ToolAnnotations): boolean {
52
91
  switch (mode) {
53
92
  case SafetyMode.READ_ONLY:
54
93
  return !!annotations.readOnlyHint;
94
+ case SafetyMode.WRITE_NONDESTRUCTIVE:
55
95
  case SafetyMode.WRITE_IDEMPOTENT:
56
96
  return !!annotations.readOnlyHint || !annotations.destructiveHint;
57
97
  case SafetyMode.WRITE_DESTRUCTIVE:
58
98
  return true;
59
99
  default:
60
- return true; // Default to most permissive if mode is unknown
100
+ return true;
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Validates if an operation is allowed in the current safety mode using semantic impact policy.
106
+ * Unknown modes fail closed.
107
+ */
108
+ export function isOperationAllowed(mode: SafetyMode | string, policy: OperationPolicy): boolean {
109
+ switch (mode) {
110
+ case SafetyMode.READ_ONLY:
111
+ return policy.impact === 'read';
112
+ case SafetyMode.WRITE_NONDESTRUCTIVE:
113
+ case SafetyMode.WRITE_IDEMPOTENT:
114
+ return (
115
+ policy.impact === 'read' ||
116
+ policy.impact === 'write-nondestructive' ||
117
+ policy.impact === 'external-side-effect'
118
+ );
119
+ case SafetyMode.WRITE_DESTRUCTIVE:
120
+ return true;
121
+ default:
122
+ return false;
61
123
  }
62
124
  }
63
125
 
64
126
  /**
65
127
  * Resolves safety mode from environment variable.
128
+ * Accepts `write-idempotent` as a deprecated alias for `write-nondestructive`.
66
129
  */
67
130
  export function getSafetyModeFromEnv(): SafetyMode {
68
131
  const mode = process.env[ENV_LIGHTDASH_TOOLS_SAFETY_MODE];
132
+ if (mode === SafetyMode.WRITE_IDEMPOTENT) {
133
+ return SafetyMode.WRITE_NONDESTRUCTIVE;
134
+ }
69
135
  if (Object.values(SafetyMode).includes(mode as SafetyMode)) {
70
136
  return mode as SafetyMode;
71
137
  }
@@ -143,7 +209,7 @@ export function extractProjectUuids(args: unknown): string[] {
143
209
  } else if (typeof args === 'object') {
144
210
  // MCP or single options object
145
211
  const a = args as Record<string, unknown>;
146
- const keys = ['project', 'projectUuid', 'projectUuids'];
212
+ const keys = ['projects', 'project', 'projectUuid', 'projectUuids'];
147
213
  for (const key of keys) {
148
214
  const val = a[key];
149
215
  if (typeof val === 'string') {
@@ -156,5 +222,7 @@ export function extractProjectUuids(args: unknown): string[] {
156
222
  }
157
223
  }
158
224
 
225
+ uuids.push(...extractProjectUuidsFromToolArgs(args));
226
+
159
227
  return [...new Set(uuids)].filter((u) => u.length > 0);
160
228
  }