@lightdash-tools/common 0.6.0 → 0.8.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 (120) 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 +217 -0
  11. package/dist/argument-validation.test.d.ts +1 -0
  12. package/dist/argument-validation.test.js +205 -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 -3
  20. package/dist/index.js +14 -2
  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 +5 -0
  25. package/dist/operations/agent-safe.js +9 -0
  26. package/dist/operations/ai-agents.d.ts +6 -0
  27. package/dist/operations/ai-agents.js +427 -0
  28. package/dist/operations/cli-content.d.ts +5 -0
  29. package/dist/operations/cli-content.js +71 -0
  30. package/dist/operations/client-coverage.d.ts +113 -0
  31. package/dist/operations/client-coverage.js +130 -0
  32. package/dist/operations/client-coverage.test.d.ts +1 -0
  33. package/dist/operations/client-coverage.test.js +22 -0
  34. package/dist/operations/content-developer.d.ts +2 -0
  35. package/dist/operations/content-developer.js +457 -0
  36. package/dist/operations/content-governance.d.ts +7 -0
  37. package/dist/operations/content-governance.js +98 -0
  38. package/dist/operations/content-reader.d.ts +2 -0
  39. package/dist/operations/content-reader.js +183 -0
  40. package/dist/operations/index.d.ts +12 -0
  41. package/dist/operations/index.js +34 -0
  42. package/dist/operations/organization-audit.d.ts +2 -0
  43. package/dist/operations/organization-audit.js +273 -0
  44. package/dist/operations/parity.test.d.ts +1 -0
  45. package/dist/operations/parity.test.js +29 -0
  46. package/dist/operations/registry.d.ts +18 -0
  47. package/dist/operations/registry.js +110 -0
  48. package/dist/operations/registry.test.d.ts +1 -0
  49. package/dist/operations/registry.test.js +223 -0
  50. package/dist/operations/semantic-layer.d.ts +6 -0
  51. package/dist/operations/semantic-layer.js +154 -0
  52. package/dist/operations/types.d.ts +86 -0
  53. package/dist/operations/types.js +104 -0
  54. package/dist/operations/types.test.d.ts +1 -0
  55. package/dist/operations/types.test.js +92 -0
  56. package/dist/operations/users.d.ts +6 -0
  57. package/dist/operations/users.js +58 -0
  58. package/dist/safety.d.ts +32 -2
  59. package/dist/safety.js +62 -5
  60. package/dist/safety.test.js +217 -4
  61. package/dist/types/generated/openapi-types.d.ts +32623 -11194
  62. package/dist/types/index.d.ts +12 -0
  63. package/dist/types/index.js +29 -0
  64. package/dist/types/lightdash-api.d.ts +1 -1
  65. package/dist/types/v1/ai-agents.d.ts +112 -3
  66. package/dist/types/v1/charts.d.ts +5 -3
  67. package/dist/types/v1/dashboards.d.ts +13 -0
  68. package/dist/types/v1/index.d.ts +45 -8
  69. package/dist/types/v1/index.js +1 -1
  70. package/dist/types/v1/lightdash-api.d.ts +107 -16
  71. package/dist/types/v1/users.d.ts +4 -0
  72. package/dist/types/v2/content.d.ts +2 -0
  73. package/dist/types/v2/content.js +8 -0
  74. package/dist/types/v2/index.d.ts +11 -9
  75. package/dist/types/v2/index.js +1 -1
  76. package/package.json +17 -3
  77. package/src/agentops/extract-yaml-project.ts +42 -0
  78. package/src/agentops/formatters.ts +52 -0
  79. package/src/agentops/snapshots.ts +56 -0
  80. package/src/agentops/types.ts +599 -0
  81. package/src/argument-validation.test.ts +301 -0
  82. package/src/argument-validation.ts +258 -0
  83. package/src/audit.test.ts +87 -0
  84. package/src/audit.ts +32 -3
  85. package/src/env.ts +9 -0
  86. package/src/index.test.ts +1 -0
  87. package/src/index.ts +17 -6
  88. package/src/input-validation.test.ts +157 -1
  89. package/src/input-validation.ts +95 -0
  90. package/src/operations/agent-safe.ts +8 -0
  91. package/src/operations/ai-agents.ts +467 -0
  92. package/src/operations/cli-content.ts +79 -0
  93. package/src/operations/client-coverage.test.ts +23 -0
  94. package/src/operations/client-coverage.ts +147 -0
  95. package/src/operations/content-developer.ts +485 -0
  96. package/src/operations/content-governance.ts +110 -0
  97. package/src/operations/content-reader.ts +197 -0
  98. package/src/operations/index.ts +42 -0
  99. package/src/operations/organization-audit.ts +293 -0
  100. package/src/operations/parity.test.ts +33 -0
  101. package/src/operations/registry.test.ts +250 -0
  102. package/src/operations/registry.ts +128 -0
  103. package/src/operations/semantic-layer.ts +166 -0
  104. package/src/operations/types.test.ts +110 -0
  105. package/src/operations/types.ts +231 -0
  106. package/src/operations/users.ts +65 -0
  107. package/src/safety.test.ts +276 -4
  108. package/src/safety.ts +83 -4
  109. package/src/types/generated/openapi-types.ts +33701 -11813
  110. package/src/types/index.ts +13 -0
  111. package/src/types/lightdash-api.ts +39 -0
  112. package/src/types/v1/ai-agents.ts +133 -3
  113. package/src/types/v1/charts.ts +5 -3
  114. package/src/types/v1/dashboards.ts +19 -0
  115. package/src/types/v1/index.ts +51 -8
  116. package/src/types/v1/lightdash-api.ts +129 -20
  117. package/src/types/v1/users.ts +4 -0
  118. package/src/types/v2/content.ts +8 -0
  119. package/src/types/v2/index.ts +11 -9
  120. package/tsconfig.json +1 -0
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Organization member operations in the shared operation catalog.
3
+ */
4
+ import type { OperationDescriptor } from './types';
5
+ /** Organization member operations registered in the shared operation catalog. */
6
+ export declare const USER_OPERATIONS: readonly OperationDescriptor[];
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ /**
3
+ * Organization member operations in the shared operation catalog.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.USER_OPERATIONS = void 0;
7
+ const safety_1 = require("../safety");
8
+ const types_1 = require("./types");
9
+ const PROFILE_DISCOVERY = 'discovery-readonly';
10
+ const PROFILE_CORE_LIFECYCLE = 'core-lifecycle';
11
+ const API_V1 = '/api/v1';
12
+ const MEMBERS_PATH = `${API_V1}/org/users`;
13
+ const MEMBER_PATH = `${MEMBERS_PATH}/{userUuid}`;
14
+ const DELETE_MEMBER_PATH = `${API_V1}/org/user/{userUuid}`;
15
+ const membersList = (0, types_1.defineOperation)({
16
+ id: 'users.members.list',
17
+ summary: 'List organization members (one page)',
18
+ http: { method: 'GET', path: MEMBERS_PATH },
19
+ authorization: { safetyImpact: 'read' },
20
+ sensitivity: 'pii.email',
21
+ mcp: {
22
+ toolName: 'list_organization_members',
23
+ annotations: safety_1.READ_ONLY_DEFAULT,
24
+ taskSupport: { exposed: false, taskEligible: false },
25
+ },
26
+ cli: { commandPath: 'users list' },
27
+ profiles: [PROFILE_DISCOVERY],
28
+ });
29
+ const membersGet = (0, types_1.defineOperation)({
30
+ id: 'users.members.get',
31
+ summary: 'Get an organization member by UUID',
32
+ http: { method: 'GET', path: MEMBER_PATH },
33
+ authorization: { safetyImpact: 'read' },
34
+ sensitivity: 'pii.email',
35
+ mcp: {
36
+ toolName: 'get_member',
37
+ annotations: safety_1.READ_ONLY_DEFAULT,
38
+ taskSupport: { exposed: false, taskEligible: false },
39
+ },
40
+ cli: { commandPath: 'users get' },
41
+ profiles: [PROFILE_DISCOVERY],
42
+ });
43
+ const membersDelete = (0, types_1.defineOperation)({
44
+ id: 'users.members.delete',
45
+ summary: 'Permanently remove a user from the organization (irrecoverable; client-only)',
46
+ http: { method: 'DELETE', path: DELETE_MEMBER_PATH },
47
+ authorization: { safetyImpact: 'write-destructive' },
48
+ sensitivity: 'none',
49
+ agentExposure: 'client-only',
50
+ bannedMcpToolName: 'delete_member',
51
+ profiles: [PROFILE_CORE_LIFECYCLE],
52
+ });
53
+ /** Organization member operations registered in the shared operation catalog. */
54
+ exports.USER_OPERATIONS = [
55
+ membersList,
56
+ membersGet,
57
+ membersDelete,
58
+ ];
package/dist/safety.d.ts CHANGED
@@ -1,9 +1,16 @@
1
+ /**
2
+ * Semantic impact classification for operations (RFC Phase 0).
3
+ * Used by {@link OperationPolicy} and {@link isOperationAllowed}.
4
+ */
5
+ export type SafetyImpact = 'credential-sensitive' | 'external-side-effect' | 'read' | 'write-destructive' | 'write-nondestructive';
1
6
  /**
2
7
  * Hierarchical safety modes for Lightdash AI tools and CLI.
3
8
  */
4
9
  export declare enum SafetyMode {
5
10
  READ_ONLY = "read-only",
11
+ /** Deprecated alias — prefer {@link SafetyMode.WRITE_NONDESTRUCTIVE}. */
6
12
  WRITE_IDEMPOTENT = "write-idempotent",
13
+ WRITE_NONDESTRUCTIVE = "write-nondestructive",
7
14
  WRITE_DESTRUCTIVE = "write-destructive"
8
15
  }
9
16
  /**
@@ -17,18 +24,41 @@ export type ToolAnnotations = {
17
24
  idempotentHint?: boolean;
18
25
  openWorldHint?: boolean;
19
26
  };
27
+ /** Policy describing the semantic impact of an operation. */
28
+ export type OperationPolicy = {
29
+ impact: SafetyImpact;
30
+ };
20
31
  /** Preset: read-only, non-destructive, idempotent, closed-world. Use for list/get/compile tools. */
21
32
  export declare const READ_ONLY_DEFAULT: ToolAnnotations;
22
- /** Preset: write, non-destructive, idempotent (e.g. upsert by slug). Use for create/update tools. */
33
+ /**
34
+ * Preset: read-only but non-idempotent (transient warehouse execution).
35
+ * Used by content-reader saved-chart/tile runs (ADR-0012).
36
+ */
37
+ export declare const READ_ONLY_TRANSIENT: ToolAnnotations;
38
+ /**
39
+ * Legacy preset: write, non-destructive, idempotent (e.g. upsert by slug).
40
+ * Prefer {@link WRITE_NONDESTRUCTIVE} for new tools; idempotent behavior is not implied by safety mode.
41
+ */
23
42
  export declare const WRITE_IDEMPOTENT: ToolAnnotations;
43
+ /** Preset: write, non-destructive, closed-world. Use for create/update tools without idempotency guarantee. */
44
+ export declare const WRITE_NONDESTRUCTIVE: ToolAnnotations;
45
+ /** Preset: write, non-destructive, open-world. Use for LLM generation and similar external interactions. */
46
+ export declare const WRITE_OPEN_WORLD: ToolAnnotations;
24
47
  /** Preset: write, destructive, non-idempotent. Use for delete/remove tools. */
25
48
  export declare const WRITE_DESTRUCTIVE: ToolAnnotations;
26
49
  /**
27
- * Validates if an operation is allowed in the current safety mode.
50
+ * Validates if an operation is allowed in the current safety mode using MCP tool annotations.
51
+ * Unknown modes default to permissive for backward compatibility.
28
52
  */
29
53
  export declare function isAllowed(mode: SafetyMode | string, annotations: ToolAnnotations): boolean;
54
+ /**
55
+ * Validates if an operation is allowed in the current safety mode using semantic impact policy.
56
+ * Unknown modes fail closed.
57
+ */
58
+ export declare function isOperationAllowed(mode: SafetyMode | string, policy: OperationPolicy): boolean;
30
59
  /**
31
60
  * Resolves safety mode from environment variable.
61
+ * Accepts `write-idempotent` as a deprecated alias for `write-nondestructive`.
32
62
  */
33
63
  export declare function getSafetyModeFromEnv(): SafetyMode;
34
64
  /**
package/dist/safety.js CHANGED
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WRITE_DESTRUCTIVE = exports.WRITE_IDEMPOTENT = exports.READ_ONLY_DEFAULT = exports.SafetyMode = void 0;
3
+ exports.WRITE_DESTRUCTIVE = exports.WRITE_OPEN_WORLD = exports.WRITE_NONDESTRUCTIVE = exports.WRITE_IDEMPOTENT = exports.READ_ONLY_TRANSIENT = exports.READ_ONLY_DEFAULT = exports.SafetyMode = void 0;
4
4
  exports.isAllowed = isAllowed;
5
+ exports.isOperationAllowed = isOperationAllowed;
5
6
  exports.getSafetyModeFromEnv = getSafetyModeFromEnv;
6
7
  exports.getAllowedProjectUuidsFromEnv = getAllowedProjectUuidsFromEnv;
7
8
  exports.isProjectAllowed = isProjectAllowed;
8
9
  exports.areAllProjectsAllowed = areAllProjectsAllowed;
9
10
  exports.extractProjectUuids = extractProjectUuids;
11
+ const extract_yaml_project_1 = require("./agentops/extract-yaml-project");
10
12
  const env_1 = require("./env");
11
13
  /**
12
14
  * Hierarchical safety modes for Lightdash AI tools and CLI.
@@ -14,7 +16,9 @@ const env_1 = require("./env");
14
16
  var SafetyMode;
15
17
  (function (SafetyMode) {
16
18
  SafetyMode["READ_ONLY"] = "read-only";
19
+ /** Deprecated alias — prefer {@link SafetyMode.WRITE_NONDESTRUCTIVE}. */
17
20
  SafetyMode["WRITE_IDEMPOTENT"] = "write-idempotent";
21
+ SafetyMode["WRITE_NONDESTRUCTIVE"] = "write-nondestructive";
18
22
  SafetyMode["WRITE_DESTRUCTIVE"] = "write-destructive";
19
23
  })(SafetyMode || (exports.SafetyMode = SafetyMode = {}));
20
24
  /** Preset: read-only, non-destructive, idempotent, closed-world. Use for list/get/compile tools. */
@@ -24,13 +28,40 @@ exports.READ_ONLY_DEFAULT = {
24
28
  destructiveHint: false,
25
29
  idempotentHint: true,
26
30
  };
27
- /** Preset: write, non-destructive, idempotent (e.g. upsert by slug). Use for create/update tools. */
31
+ /**
32
+ * Preset: read-only but non-idempotent (transient warehouse execution).
33
+ * Used by content-reader saved-chart/tile runs (ADR-0012).
34
+ */
35
+ exports.READ_ONLY_TRANSIENT = {
36
+ readOnlyHint: true,
37
+ openWorldHint: false,
38
+ destructiveHint: false,
39
+ idempotentHint: false,
40
+ };
41
+ /**
42
+ * Legacy preset: write, non-destructive, idempotent (e.g. upsert by slug).
43
+ * Prefer {@link WRITE_NONDESTRUCTIVE} for new tools; idempotent behavior is not implied by safety mode.
44
+ */
28
45
  exports.WRITE_IDEMPOTENT = {
29
46
  readOnlyHint: false,
30
47
  openWorldHint: false,
31
48
  destructiveHint: false,
32
49
  idempotentHint: true,
33
50
  };
51
+ /** Preset: write, non-destructive, closed-world. Use for create/update tools without idempotency guarantee. */
52
+ exports.WRITE_NONDESTRUCTIVE = {
53
+ readOnlyHint: false,
54
+ openWorldHint: false,
55
+ destructiveHint: false,
56
+ idempotentHint: false,
57
+ };
58
+ /** Preset: write, non-destructive, open-world. Use for LLM generation and similar external interactions. */
59
+ exports.WRITE_OPEN_WORLD = {
60
+ readOnlyHint: false,
61
+ openWorldHint: true,
62
+ destructiveHint: false,
63
+ idempotentHint: false,
64
+ };
34
65
  /** Preset: write, destructive, non-idempotent. Use for delete/remove tools. */
35
66
  exports.WRITE_DESTRUCTIVE = {
36
67
  readOnlyHint: false,
@@ -39,25 +70,50 @@ exports.WRITE_DESTRUCTIVE = {
39
70
  idempotentHint: false,
40
71
  };
41
72
  /**
42
- * Validates if an operation is allowed in the current safety mode.
73
+ * Validates if an operation is allowed in the current safety mode using MCP tool annotations.
74
+ * Unknown modes default to permissive for backward compatibility.
43
75
  */
44
76
  function isAllowed(mode, annotations) {
45
77
  switch (mode) {
46
78
  case SafetyMode.READ_ONLY:
47
79
  return !!annotations.readOnlyHint;
80
+ case SafetyMode.WRITE_NONDESTRUCTIVE:
48
81
  case SafetyMode.WRITE_IDEMPOTENT:
49
82
  return !!annotations.readOnlyHint || !annotations.destructiveHint;
50
83
  case SafetyMode.WRITE_DESTRUCTIVE:
51
84
  return true;
52
85
  default:
53
- return true; // Default to most permissive if mode is unknown
86
+ return true;
87
+ }
88
+ }
89
+ /**
90
+ * Validates if an operation is allowed in the current safety mode using semantic impact policy.
91
+ * Unknown modes fail closed.
92
+ */
93
+ function isOperationAllowed(mode, policy) {
94
+ switch (mode) {
95
+ case SafetyMode.READ_ONLY:
96
+ return policy.impact === 'read';
97
+ case SafetyMode.WRITE_NONDESTRUCTIVE:
98
+ case SafetyMode.WRITE_IDEMPOTENT:
99
+ return (policy.impact === 'read' ||
100
+ policy.impact === 'write-nondestructive' ||
101
+ policy.impact === 'external-side-effect');
102
+ case SafetyMode.WRITE_DESTRUCTIVE:
103
+ return true;
104
+ default:
105
+ return false;
54
106
  }
55
107
  }
56
108
  /**
57
109
  * Resolves safety mode from environment variable.
110
+ * Accepts `write-idempotent` as a deprecated alias for `write-nondestructive`.
58
111
  */
59
112
  function getSafetyModeFromEnv() {
60
113
  const mode = process.env[env_1.ENV_LIGHTDASH_TOOLS_SAFETY_MODE];
114
+ if (mode === SafetyMode.WRITE_IDEMPOTENT) {
115
+ return SafetyMode.WRITE_NONDESTRUCTIVE;
116
+ }
61
117
  if (Object.values(SafetyMode).includes(mode)) {
62
118
  return mode;
63
119
  }
@@ -135,7 +191,7 @@ function extractProjectUuids(args) {
135
191
  else if (typeof args === 'object') {
136
192
  // MCP or single options object
137
193
  const a = args;
138
- const keys = ['project', 'projectUuid', 'projectUuids'];
194
+ const keys = ['projects', 'project', 'projectUuid', 'projectUuids'];
139
195
  for (const key of keys) {
140
196
  const val = a[key];
141
197
  if (typeof val === 'string') {
@@ -149,5 +205,6 @@ function extractProjectUuids(args) {
149
205
  }
150
206
  }
151
207
  }
208
+ uuids.push(...(0, extract_yaml_project_1.extractProjectUuidsFromToolArgs)(args));
152
209
  return [...new Set(uuids)].filter((u) => u.length > 0);
153
210
  }
@@ -2,27 +2,135 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const safety_1 = require("./safety");
5
+ const UUID_A = '11111111-1111-1111-1111-111111111111';
6
+ const UUID_B = '22222222-2222-2222-2222-222222222222';
7
+ const UUID_C = '33333333-3333-3333-3333-333333333333';
8
+ const ALL_IMPACTS = [
9
+ 'read',
10
+ 'write-nondestructive',
11
+ 'write-destructive',
12
+ 'credential-sensitive',
13
+ 'external-side-effect',
14
+ ];
15
+ function policy(impact) {
16
+ return { impact };
17
+ }
5
18
  (0, vitest_1.describe)('Safety Logic', () => {
19
+ (0, vitest_1.describe)('annotation presets', () => {
20
+ (0, vitest_1.it)('should define READ_ONLY_DEFAULT as read-only closed-world', () => {
21
+ (0, vitest_1.expect)(safety_1.READ_ONLY_DEFAULT).toEqual({
22
+ readOnlyHint: true,
23
+ openWorldHint: false,
24
+ destructiveHint: false,
25
+ idempotentHint: true,
26
+ });
27
+ });
28
+ (0, vitest_1.it)('should define WRITE_IDEMPOTENT as deprecated non-destructive idempotent write', () => {
29
+ (0, vitest_1.expect)(safety_1.WRITE_IDEMPOTENT).toEqual({
30
+ readOnlyHint: false,
31
+ openWorldHint: false,
32
+ destructiveHint: false,
33
+ idempotentHint: true,
34
+ });
35
+ });
36
+ (0, vitest_1.it)('should define WRITE_NONDESTRUCTIVE as non-destructive closed-world write', () => {
37
+ (0, vitest_1.expect)(safety_1.WRITE_NONDESTRUCTIVE).toEqual({
38
+ readOnlyHint: false,
39
+ openWorldHint: false,
40
+ destructiveHint: false,
41
+ idempotentHint: false,
42
+ });
43
+ });
44
+ (0, vitest_1.it)('should define WRITE_OPEN_WORLD for LLM generation style tools', () => {
45
+ (0, vitest_1.expect)(safety_1.WRITE_OPEN_WORLD).toEqual({
46
+ readOnlyHint: false,
47
+ openWorldHint: true,
48
+ destructiveHint: false,
49
+ idempotentHint: false,
50
+ });
51
+ });
52
+ (0, vitest_1.it)('should define WRITE_DESTRUCTIVE as destructive write', () => {
53
+ (0, vitest_1.expect)(safety_1.WRITE_DESTRUCTIVE).toEqual({
54
+ readOnlyHint: false,
55
+ openWorldHint: false,
56
+ destructiveHint: true,
57
+ idempotentHint: false,
58
+ });
59
+ });
60
+ });
61
+ (0, vitest_1.describe)('SafetyMode enum', () => {
62
+ (0, vitest_1.it)('should keep WRITE_IDEMPOTENT as deprecated alias with distinct string value', () => {
63
+ (0, vitest_1.expect)(safety_1.SafetyMode.WRITE_IDEMPOTENT).toBe('write-idempotent');
64
+ (0, vitest_1.expect)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE).toBe('write-nondestructive');
65
+ (0, vitest_1.expect)(safety_1.SafetyMode.WRITE_IDEMPOTENT).not.toBe(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE);
66
+ });
67
+ });
6
68
  (0, vitest_1.describe)('isAllowed', () => {
7
69
  (0, vitest_1.it)('should allow read-only operations in read-only mode', () => {
8
70
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.READ_ONLY, safety_1.READ_ONLY_DEFAULT)).toBe(true);
9
71
  });
10
72
  (0, vitest_1.it)('should forbid non-read-only operations in read-only mode', () => {
11
73
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.READ_ONLY, safety_1.WRITE_IDEMPOTENT)).toBe(false);
74
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.READ_ONLY, safety_1.WRITE_NONDESTRUCTIVE)).toBe(false);
75
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.READ_ONLY, safety_1.WRITE_OPEN_WORLD)).toBe(false);
12
76
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.READ_ONLY, safety_1.WRITE_DESTRUCTIVE)).toBe(false);
13
77
  });
14
- (0, vitest_1.it)('should allow read-only and idempotent-write in write-idempotent mode', () => {
78
+ (0, vitest_1.it)('should allow read-only and non-destructive writes in write-idempotent mode', () => {
15
79
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, safety_1.READ_ONLY_DEFAULT)).toBe(true);
16
80
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, safety_1.WRITE_IDEMPOTENT)).toBe(true);
81
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, safety_1.WRITE_NONDESTRUCTIVE)).toBe(true);
82
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, safety_1.WRITE_OPEN_WORLD)).toBe(true);
17
83
  });
18
- (0, vitest_1.it)('should forbid destructive operations in write-idempotent mode', () => {
84
+ (0, vitest_1.it)('should allow read-only and non-destructive writes in write-nondestructive mode', () => {
85
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, safety_1.READ_ONLY_DEFAULT)).toBe(true);
86
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, safety_1.WRITE_NONDESTRUCTIVE)).toBe(true);
87
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, safety_1.WRITE_OPEN_WORLD)).toBe(true);
88
+ });
89
+ (0, vitest_1.it)('should forbid destructive operations in write-idempotent and write-nondestructive modes', () => {
19
90
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, safety_1.WRITE_DESTRUCTIVE)).toBe(false);
91
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, safety_1.WRITE_DESTRUCTIVE)).toBe(false);
20
92
  });
21
93
  (0, vitest_1.it)('should allow all operations in write-destructive mode', () => {
22
94
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, safety_1.READ_ONLY_DEFAULT)).toBe(true);
23
95
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, safety_1.WRITE_IDEMPOTENT)).toBe(true);
96
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, safety_1.WRITE_NONDESTRUCTIVE)).toBe(true);
97
+ (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, safety_1.WRITE_OPEN_WORLD)).toBe(true);
24
98
  (0, vitest_1.expect)((0, safety_1.isAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, safety_1.WRITE_DESTRUCTIVE)).toBe(true);
25
99
  });
100
+ (0, vitest_1.it)('should default to permissive for an unknown mode string (backward compat)', () => {
101
+ (0, vitest_1.expect)((0, safety_1.isAllowed)('custom-mode', safety_1.WRITE_DESTRUCTIVE)).toBe(true);
102
+ });
103
+ });
104
+ (0, vitest_1.describe)('isOperationAllowed', () => {
105
+ (0, vitest_1.it)('should allow only read impact in read-only mode', () => {
106
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('read'))).toBe(true);
107
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('write-nondestructive'))).toBe(false);
108
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('write-destructive'))).toBe(false);
109
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('credential-sensitive'))).toBe(false);
110
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.READ_ONLY, policy('external-side-effect'))).toBe(false);
111
+ });
112
+ (0, vitest_1.it)('should allow read, write-nondestructive, and external-side-effect in write-nondestructive mode', () => {
113
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('read'))).toBe(true);
114
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-nondestructive'))).toBe(true);
115
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('external-side-effect'))).toBe(true);
116
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('write-destructive'))).toBe(false);
117
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy('credential-sensitive'))).toBe(false);
118
+ });
119
+ (0, vitest_1.it)('should treat write-idempotent mode the same as write-nondestructive for policy checks', () => {
120
+ for (const impact of ALL_IMPACTS) {
121
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_IDEMPOTENT, policy(impact))).toBe((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE, policy(impact)));
122
+ }
123
+ });
124
+ (0, vitest_1.it)('should allow all impacts in write-destructive mode', () => {
125
+ for (const impact of ALL_IMPACTS) {
126
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)(safety_1.SafetyMode.WRITE_DESTRUCTIVE, policy(impact))).toBe(true);
127
+ }
128
+ });
129
+ (0, vitest_1.it)('should fail closed for unknown mode strings', () => {
130
+ for (const impact of ALL_IMPACTS) {
131
+ (0, vitest_1.expect)((0, safety_1.isOperationAllowed)('custom-mode', policy(impact))).toBe(false);
132
+ }
133
+ });
26
134
  });
27
135
  (0, vitest_1.describe)('getSafetyModeFromEnv', () => {
28
136
  const originalEnv = process.env.LIGHTDASH_TOOLS_SAFETY_MODE;
@@ -33,9 +141,17 @@ const safety_1 = require("./safety");
33
141
  delete process.env.LIGHTDASH_TOOLS_SAFETY_MODE;
34
142
  (0, vitest_1.expect)((0, safety_1.getSafetyModeFromEnv)()).toBe(safety_1.SafetyMode.READ_ONLY);
35
143
  });
36
- (0, vitest_1.it)('should return value from env when set to valid mode', () => {
144
+ (0, vitest_1.it)('should return WRITE_NONDESTRUCTIVE when env is set to write-nondestructive', () => {
145
+ process.env.LIGHTDASH_TOOLS_SAFETY_MODE = safety_1.SafetyMode.WRITE_NONDESTRUCTIVE;
146
+ (0, vitest_1.expect)((0, safety_1.getSafetyModeFromEnv)()).toBe(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE);
147
+ });
148
+ (0, vitest_1.it)('should map deprecated write-idempotent env value to WRITE_NONDESTRUCTIVE', () => {
37
149
  process.env.LIGHTDASH_TOOLS_SAFETY_MODE = safety_1.SafetyMode.WRITE_IDEMPOTENT;
38
- (0, vitest_1.expect)((0, safety_1.getSafetyModeFromEnv)()).toBe(safety_1.SafetyMode.WRITE_IDEMPOTENT);
150
+ (0, vitest_1.expect)((0, safety_1.getSafetyModeFromEnv)()).toBe(safety_1.SafetyMode.WRITE_NONDESTRUCTIVE);
151
+ });
152
+ (0, vitest_1.it)('should return WRITE_DESTRUCTIVE when env is set to write-destructive', () => {
153
+ process.env.LIGHTDASH_TOOLS_SAFETY_MODE = safety_1.SafetyMode.WRITE_DESTRUCTIVE;
154
+ (0, vitest_1.expect)((0, safety_1.getSafetyModeFromEnv)()).toBe(safety_1.SafetyMode.WRITE_DESTRUCTIVE);
39
155
  });
40
156
  (0, vitest_1.it)('should return READ_ONLY when env is set to invalid value', () => {
41
157
  process.env.LIGHTDASH_TOOLS_SAFETY_MODE = 'invalid-mode';
@@ -96,4 +212,101 @@ const safety_1 = require("./safety");
96
212
  (0, vitest_1.expect)((0, safety_1.areAllProjectsAllowed)(['uuid-a'], ['uuid-b', 'uuid-c'])).toBe(false);
97
213
  });
98
214
  });
215
+ (0, vitest_1.describe)('extractProjectUuids', () => {
216
+ (0, vitest_1.it)('should return empty array for null', () => {
217
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)(null)).toEqual([]);
218
+ });
219
+ (0, vitest_1.it)('should return empty array for undefined', () => {
220
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)(undefined)).toEqual([]);
221
+ });
222
+ (0, vitest_1.it)('should extract projectUuid from MCP-style object', () => {
223
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ projectUuid: UUID_A })).toEqual([UUID_A]);
224
+ });
225
+ (0, vitest_1.it)('should extract projectUuids array from MCP-style object', () => {
226
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ projectUuids: [UUID_A, UUID_B] })).toEqual([UUID_A, UUID_B]);
227
+ });
228
+ (0, vitest_1.it)('should extract projects array from MCP-style object', () => {
229
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ projects: [UUID_A, UUID_B] })).toEqual([UUID_A, UUID_B]);
230
+ });
231
+ (0, vitest_1.it)('should extract project from MCP-style object', () => {
232
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ project: UUID_A })).toEqual([UUID_A]);
233
+ });
234
+ (0, vitest_1.it)('should extract UUID from CLI positional argument', () => {
235
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([UUID_A])).toEqual([UUID_A]);
236
+ });
237
+ (0, vitest_1.it)('should ignore non-UUID CLI positional strings', () => {
238
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)(['not-a-uuid', 'also-invalid'])).toEqual([]);
239
+ });
240
+ (0, vitest_1.it)('should extract from nested CLI options with projects key', () => {
241
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([{ projects: [UUID_A, UUID_B] }])).toEqual([UUID_A, UUID_B]);
242
+ });
243
+ (0, vitest_1.it)('should extract from nested CLI options with project key', () => {
244
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([{ project: UUID_A }])).toEqual([UUID_A]);
245
+ });
246
+ (0, vitest_1.it)('should extract from nested CLI options with projectUuid key', () => {
247
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([{ projectUuid: UUID_A }])).toEqual([UUID_A]);
248
+ });
249
+ (0, vitest_1.it)('should extract from nested CLI options with projectUuids key', () => {
250
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([{ projectUuids: [UUID_A, UUID_B] }])).toEqual([UUID_A, UUID_B]);
251
+ });
252
+ (0, vitest_1.it)('should combine positional UUIDs and nested option values', () => {
253
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([UUID_A, { project: UUID_B }])).toEqual([UUID_A, UUID_B]);
254
+ });
255
+ (0, vitest_1.it)('should deduplicate UUIDs across sources', () => {
256
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([
257
+ UUID_A,
258
+ { projectUuid: UUID_A, projectUuids: [UUID_B, UUID_C] },
259
+ { projects: [UUID_B, UUID_C] },
260
+ ])).toEqual([UUID_A, UUID_B, UUID_C]);
261
+ });
262
+ (0, vitest_1.it)('should filter out empty strings from extracted values', () => {
263
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({
264
+ projectUuid: '',
265
+ projectUuids: [UUID_A, '', UUID_B],
266
+ })).toEqual([UUID_A, UUID_B]);
267
+ });
268
+ (0, vitest_1.it)('should filter empty strings from nested CLI options', () => {
269
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([{ projects: ['', UUID_A, ''] }])).toEqual([UUID_A]);
270
+ });
271
+ (0, vitest_1.it)('should return empty array for MCP object with no project keys', () => {
272
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ query: 'test', limit: 10 })).toEqual([]);
273
+ });
274
+ (0, vitest_1.it)('should extract projectUuid from bundleYaml in MCP tool args', () => {
275
+ const projectUuid = '550e8400-e29b-41d4-a716-446655440000';
276
+ const bundleYaml = [
277
+ 'apiVersion: lightdash.ai/v1alpha1',
278
+ 'kind: LightdashAiAgentBundle',
279
+ 'metadata:',
280
+ ' name: test-bundle',
281
+ 'spec:',
282
+ ` projectUuid: ${projectUuid}`,
283
+ ' agents:',
284
+ ' - key: a1',
285
+ ' name: Agent One',
286
+ ' evaluations: []',
287
+ ].join('\n');
288
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ bundleYaml })).toEqual([projectUuid]);
289
+ });
290
+ (0, vitest_1.it)('should extract projectUuid from gateYaml in MCP tool args', () => {
291
+ const projectUuid = '550e8400-e29b-41d4-a716-446655440000';
292
+ const agentUuid = '660e8400-e29b-41d4-a716-446655440001';
293
+ const evaluationUuid = '770e8400-e29b-41d4-a716-446655440002';
294
+ const gateYaml = [
295
+ 'apiVersion: lightdash.ai/v1alpha1',
296
+ 'kind: LightdashAiEvaluationGate',
297
+ 'metadata:',
298
+ ' name: release-gate',
299
+ 'spec:',
300
+ ` projectUuid: ${projectUuid}`,
301
+ ` agentUuid: ${agentUuid}`,
302
+ ` evaluationUuid: ${evaluationUuid}`,
303
+ ' policy:',
304
+ ' minPassRate: 0.9',
305
+ ].join('\n');
306
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)({ gateYaml })).toEqual([projectUuid]);
307
+ });
308
+ (0, vitest_1.it)('should return empty array for empty CLI argument array', () => {
309
+ (0, vitest_1.expect)((0, safety_1.extractProjectUuids)([])).toEqual([]);
310
+ });
311
+ });
99
312
  });