@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.
- package/dist/agentops/extract-yaml-project.d.ts +7 -0
- package/dist/agentops/extract-yaml-project.js +42 -0
- package/dist/agentops/formatters.d.ts +5 -0
- package/dist/agentops/formatters.js +47 -0
- package/dist/agentops/snapshots.d.ts +32 -0
- package/dist/agentops/snapshots.js +34 -0
- package/dist/agentops/types.d.ts +224 -0
- package/dist/agentops/types.js +430 -0
- package/dist/argument-validation.d.ts +25 -0
- package/dist/argument-validation.js +217 -0
- package/dist/argument-validation.test.d.ts +1 -0
- package/dist/argument-validation.test.js +205 -0
- package/dist/audit.d.ts +9 -2
- package/dist/audit.js +17 -2
- package/dist/audit.test.d.ts +1 -0
- package/dist/audit.test.js +73 -0
- package/dist/env.d.ts +8 -0
- package/dist/env.js +9 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +14 -2
- package/dist/input-validation.d.ts +35 -0
- package/dist/input-validation.js +87 -0
- package/dist/input-validation.test.js +93 -0
- package/dist/operations/agent-safe.d.ts +5 -0
- package/dist/operations/agent-safe.js +9 -0
- package/dist/operations/ai-agents.d.ts +6 -0
- package/dist/operations/ai-agents.js +427 -0
- package/dist/operations/cli-content.d.ts +5 -0
- package/dist/operations/cli-content.js +71 -0
- package/dist/operations/client-coverage.d.ts +113 -0
- package/dist/operations/client-coverage.js +130 -0
- package/dist/operations/client-coverage.test.d.ts +1 -0
- package/dist/operations/client-coverage.test.js +22 -0
- package/dist/operations/content-developer.d.ts +2 -0
- package/dist/operations/content-developer.js +457 -0
- package/dist/operations/content-governance.d.ts +7 -0
- package/dist/operations/content-governance.js +98 -0
- package/dist/operations/content-reader.d.ts +2 -0
- package/dist/operations/content-reader.js +183 -0
- package/dist/operations/index.d.ts +12 -0
- package/dist/operations/index.js +34 -0
- package/dist/operations/organization-audit.d.ts +2 -0
- package/dist/operations/organization-audit.js +273 -0
- package/dist/operations/parity.test.d.ts +1 -0
- package/dist/operations/parity.test.js +29 -0
- package/dist/operations/registry.d.ts +18 -0
- package/dist/operations/registry.js +110 -0
- package/dist/operations/registry.test.d.ts +1 -0
- package/dist/operations/registry.test.js +223 -0
- package/dist/operations/semantic-layer.d.ts +6 -0
- package/dist/operations/semantic-layer.js +154 -0
- package/dist/operations/types.d.ts +86 -0
- package/dist/operations/types.js +104 -0
- package/dist/operations/types.test.d.ts +1 -0
- package/dist/operations/types.test.js +92 -0
- package/dist/operations/users.d.ts +6 -0
- package/dist/operations/users.js +58 -0
- package/dist/safety.d.ts +32 -2
- package/dist/safety.js +62 -5
- package/dist/safety.test.js +217 -4
- package/dist/types/generated/openapi-types.d.ts +32623 -11194
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.js +29 -0
- package/dist/types/lightdash-api.d.ts +1 -1
- package/dist/types/v1/ai-agents.d.ts +112 -3
- package/dist/types/v1/charts.d.ts +5 -3
- package/dist/types/v1/dashboards.d.ts +13 -0
- package/dist/types/v1/index.d.ts +45 -8
- package/dist/types/v1/index.js +1 -1
- package/dist/types/v1/lightdash-api.d.ts +107 -16
- package/dist/types/v1/users.d.ts +4 -0
- package/dist/types/v2/content.d.ts +2 -0
- package/dist/types/v2/content.js +8 -0
- package/dist/types/v2/index.d.ts +11 -9
- package/dist/types/v2/index.js +1 -1
- package/package.json +17 -3
- package/src/agentops/extract-yaml-project.ts +42 -0
- package/src/agentops/formatters.ts +52 -0
- package/src/agentops/snapshots.ts +56 -0
- package/src/agentops/types.ts +599 -0
- package/src/argument-validation.test.ts +301 -0
- package/src/argument-validation.ts +258 -0
- package/src/audit.test.ts +87 -0
- package/src/audit.ts +32 -3
- package/src/env.ts +9 -0
- package/src/index.test.ts +1 -0
- package/src/index.ts +17 -6
- package/src/input-validation.test.ts +157 -1
- package/src/input-validation.ts +95 -0
- package/src/operations/agent-safe.ts +8 -0
- package/src/operations/ai-agents.ts +467 -0
- package/src/operations/cli-content.ts +79 -0
- package/src/operations/client-coverage.test.ts +23 -0
- package/src/operations/client-coverage.ts +147 -0
- package/src/operations/content-developer.ts +485 -0
- package/src/operations/content-governance.ts +110 -0
- package/src/operations/content-reader.ts +197 -0
- package/src/operations/index.ts +42 -0
- package/src/operations/organization-audit.ts +293 -0
- package/src/operations/parity.test.ts +33 -0
- package/src/operations/registry.test.ts +250 -0
- package/src/operations/registry.ts +128 -0
- package/src/operations/semantic-layer.ts +166 -0
- package/src/operations/types.test.ts +110 -0
- package/src/operations/types.ts +231 -0
- package/src/operations/users.ts +65 -0
- package/src/safety.test.ts +276 -4
- package/src/safety.ts +83 -4
- package/src/types/generated/openapi-types.ts +33701 -11813
- package/src/types/index.ts +13 -0
- package/src/types/lightdash-api.ts +39 -0
- package/src/types/v1/ai-agents.ts +133 -3
- package/src/types/v1/charts.ts +5 -3
- package/src/types/v1/dashboards.ts +19 -0
- package/src/types/v1/index.ts +51 -8
- package/src/types/v1/lightdash-api.ts +129 -20
- package/src/types/v1/users.ts +4 -0
- package/src/types/v2/content.ts +8 -0
- package/src/types/v2/index.ts +11 -9
- package/tsconfig.json +1 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared typed operation catalog (ADR-0013).
|
|
3
|
+
* Single source of truth for HTTP, CLI, and MCP exposure of Lightdash API operations.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { SafetyImpact, ToolAnnotations } from '../safety';
|
|
7
|
+
|
|
8
|
+
export type { SafetyImpact };
|
|
9
|
+
|
|
10
|
+
/** Whether an operation may appear on MCP/CLI agent surfaces. */
|
|
11
|
+
export type AgentExposure = 'agent' | 'client-only';
|
|
12
|
+
|
|
13
|
+
/** HTTP verbs supported by registered operations. */
|
|
14
|
+
export type HttpMethod = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Capability profiles group operations for selective exposure (CLI profiles, MCP subsets).
|
|
18
|
+
*/
|
|
19
|
+
export type CapabilityProfile =
|
|
20
|
+
| 'content-developer'
|
|
21
|
+
| 'content-governance'
|
|
22
|
+
| 'content-reader'
|
|
23
|
+
| 'conversations'
|
|
24
|
+
| 'core-lifecycle'
|
|
25
|
+
| 'discovery-readonly'
|
|
26
|
+
| 'evaluations'
|
|
27
|
+
| 'org-audit-readonly'
|
|
28
|
+
| 'semantic-discovery';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* MCP response sensitivity class (ADR-0011).
|
|
32
|
+
* Documents which redaction class handlers must apply; does not auto-redact.
|
|
33
|
+
*/
|
|
34
|
+
export type SensitivityClass = 'none' | 'pii.email' | 'secret.connection' | 'secret.destination';
|
|
35
|
+
|
|
36
|
+
/** MCP task semantics for an operation. */
|
|
37
|
+
export type McpTaskSupport = {
|
|
38
|
+
/** When true, the operation is exposed as an MCP tool. */
|
|
39
|
+
exposed: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* When true, the operation may complete asynchronously via MCP Tasks.
|
|
42
|
+
* Long-running evaluation runs and LLM generation are task-eligible.
|
|
43
|
+
*/
|
|
44
|
+
taskEligible: boolean;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type OperationAuthorization = {
|
|
48
|
+
safetyImpact: SafetyImpact;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type OperationHttp = {
|
|
52
|
+
method: HttpMethod;
|
|
53
|
+
/** OpenAPI-style path template (includes `/api/v1` or `/api/v2` prefix). */
|
|
54
|
+
path: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type OperationMcp = {
|
|
58
|
+
/** MCP tool name without the `lightdash_` prefix. */
|
|
59
|
+
toolName: string;
|
|
60
|
+
annotations: ToolAnnotations;
|
|
61
|
+
taskSupport: McpTaskSupport;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export type OperationCli = {
|
|
65
|
+
/** Space-delimited CLI command path from the root program (e.g. `agents list`). */
|
|
66
|
+
commandPath: string;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** Optional multi-step HTTP workflow when a single registry entry orchestrates several API calls. */
|
|
70
|
+
export type OperationWorkflowStep = {
|
|
71
|
+
method: HttpMethod;
|
|
72
|
+
path: string;
|
|
73
|
+
summary: string;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type OperationDescriptor = {
|
|
77
|
+
/** Stable dot-separated identifier (e.g. `ai-agents.project.agents.list`). */
|
|
78
|
+
id: string;
|
|
79
|
+
summary: string;
|
|
80
|
+
http: OperationHttp;
|
|
81
|
+
authorization: OperationAuthorization;
|
|
82
|
+
/**
|
|
83
|
+
* Response sensitivity class for agent surfaces (ADR-0011).
|
|
84
|
+
* Defaults to `none` when omitted from `defineOperation` input.
|
|
85
|
+
*/
|
|
86
|
+
sensitivity: SensitivityClass;
|
|
87
|
+
profiles: readonly CapabilityProfile[];
|
|
88
|
+
/** `agent` (default) = may appear on MCP and/or CLI; `client-only` = typed client only. */
|
|
89
|
+
agentExposure: AgentExposure;
|
|
90
|
+
/** Present when the operation is registered (or reserved) as an MCP tool. */
|
|
91
|
+
mcp?: OperationMcp;
|
|
92
|
+
/** Present when a real CLI command exists (`commandPath` must match Commander). */
|
|
93
|
+
cli?: OperationCli;
|
|
94
|
+
/** Irrecoverable denylist name for client-only ops (sans `lightdash_` prefix). */
|
|
95
|
+
bannedMcpToolName?: string;
|
|
96
|
+
/** When set, documents the ordered client-side HTTP steps behind this operation. */
|
|
97
|
+
workflow?: readonly OperationWorkflowStep[];
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** Input to `defineOperation` before defaults are applied. */
|
|
101
|
+
export type OperationDefinitionInput = Omit<
|
|
102
|
+
OperationDescriptor,
|
|
103
|
+
'agentExposure' | 'sensitivity'
|
|
104
|
+
> & {
|
|
105
|
+
agentExposure?: AgentExposure;
|
|
106
|
+
sensitivity?: SensitivityClass;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const VALID_PROFILES = new Set<CapabilityProfile>([
|
|
110
|
+
'content-developer',
|
|
111
|
+
'content-governance',
|
|
112
|
+
'content-reader',
|
|
113
|
+
'conversations',
|
|
114
|
+
'core-lifecycle',
|
|
115
|
+
'discovery-readonly',
|
|
116
|
+
'evaluations',
|
|
117
|
+
'org-audit-readonly',
|
|
118
|
+
'semantic-discovery',
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
const VALID_SENSITIVITY = new Set<SensitivityClass>([
|
|
122
|
+
'none',
|
|
123
|
+
'pii.email',
|
|
124
|
+
'secret.connection',
|
|
125
|
+
'secret.destination',
|
|
126
|
+
]);
|
|
127
|
+
|
|
128
|
+
function assertNonEmpty(value: string, field: string): void {
|
|
129
|
+
if (value.trim().length === 0) {
|
|
130
|
+
throw new Error(`Operation ${field} must be a non-empty string`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function expectedImpactFromAnnotations(annotations: ToolAnnotations): SafetyImpact {
|
|
135
|
+
if (annotations.readOnlyHint) {
|
|
136
|
+
return 'read';
|
|
137
|
+
}
|
|
138
|
+
if (annotations.destructiveHint) {
|
|
139
|
+
return 'write-destructive';
|
|
140
|
+
}
|
|
141
|
+
if (annotations.openWorldHint) {
|
|
142
|
+
return 'external-side-effect';
|
|
143
|
+
}
|
|
144
|
+
return 'write-nondestructive';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function validateIdempotentHint(
|
|
148
|
+
annotations: ToolAnnotations,
|
|
149
|
+
impact: SafetyImpact,
|
|
150
|
+
id: string,
|
|
151
|
+
): void {
|
|
152
|
+
if (impact === 'write-destructive' && annotations.idempotentHint === true) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`Operation '${id}': destructive operations must not set mcp.annotations.idempotentHint = true`,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function validateProfiles(id: string, profiles: readonly CapabilityProfile[]): void {
|
|
160
|
+
if (profiles.length === 0) {
|
|
161
|
+
throw new Error(`Operation '${id}' must declare at least one capability profile`);
|
|
162
|
+
}
|
|
163
|
+
for (const profile of profiles) {
|
|
164
|
+
if (!VALID_PROFILES.has(profile)) {
|
|
165
|
+
throw new Error(`Operation '${id}' has unknown capability profile '${profile}'`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function validateClientOnlyOperation(input: OperationDefinitionInput): OperationDescriptor {
|
|
171
|
+
if (input.mcp !== undefined || input.cli !== undefined) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`Operation '${input.id}': client-only operations must omit mcp and cli metadata`,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
if (input.bannedMcpToolName !== undefined && input.bannedMcpToolName.trim().length === 0) {
|
|
177
|
+
throw new Error(`Operation '${input.id}': bannedMcpToolName must be non-empty when set`);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
...input,
|
|
181
|
+
agentExposure: 'client-only',
|
|
182
|
+
sensitivity: input.sensitivity ?? 'none',
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function validateAgentMcp(input: OperationDefinitionInput): void {
|
|
187
|
+
if (input.mcp === undefined) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
assertNonEmpty(input.mcp.toolName, 'mcp.toolName');
|
|
191
|
+
const expectedImpact = expectedImpactFromAnnotations(input.mcp.annotations);
|
|
192
|
+
if (input.authorization.safetyImpact !== expectedImpact) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`Operation '${input.id}': authorization.safetyImpact is '${input.authorization.safetyImpact}' but MCP annotations imply '${expectedImpact}'`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
validateIdempotentHint(input.mcp.annotations, input.authorization.safetyImpact, input.id);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Defines a typed operation descriptor with catalog consistency checks (ADR-0013).
|
|
202
|
+
* Agent ops require at least one of `mcp` or `cli`. Client-only ops omit both.
|
|
203
|
+
*/
|
|
204
|
+
export function defineOperation(input: OperationDefinitionInput): OperationDescriptor {
|
|
205
|
+
assertNonEmpty(input.id, 'id');
|
|
206
|
+
assertNonEmpty(input.summary, 'summary');
|
|
207
|
+
assertNonEmpty(input.http.path, 'http.path');
|
|
208
|
+
|
|
209
|
+
const sensitivity = input.sensitivity ?? 'none';
|
|
210
|
+
if (!VALID_SENSITIVITY.has(sensitivity)) {
|
|
211
|
+
throw new Error(`Operation '${input.id}' has unknown sensitivity '${String(sensitivity)}'`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
validateProfiles(input.id, input.profiles);
|
|
215
|
+
|
|
216
|
+
const agentExposure = input.agentExposure ?? 'agent';
|
|
217
|
+
if (agentExposure === 'client-only') {
|
|
218
|
+
return validateClientOnlyOperation({ ...input, sensitivity });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (input.mcp === undefined && input.cli === undefined) {
|
|
222
|
+
throw new Error(`Operation '${input.id}': agent operations require mcp and/or cli metadata`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
validateAgentMcp(input);
|
|
226
|
+
if (input.cli !== undefined) {
|
|
227
|
+
assertNonEmpty(input.cli.commandPath, 'cli.commandPath');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return { ...input, agentExposure: 'agent', sensitivity };
|
|
231
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Organization member operations in the shared operation catalog.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { READ_ONLY_DEFAULT } from '../safety';
|
|
6
|
+
|
|
7
|
+
import { defineOperation } from './types';
|
|
8
|
+
|
|
9
|
+
import type { CapabilityProfile, OperationDescriptor } from './types';
|
|
10
|
+
|
|
11
|
+
const PROFILE_DISCOVERY: CapabilityProfile = 'discovery-readonly';
|
|
12
|
+
const PROFILE_CORE_LIFECYCLE: CapabilityProfile = 'core-lifecycle';
|
|
13
|
+
|
|
14
|
+
const API_V1 = '/api/v1';
|
|
15
|
+
const MEMBERS_PATH = `${API_V1}/org/users`;
|
|
16
|
+
const MEMBER_PATH = `${MEMBERS_PATH}/{userUuid}`;
|
|
17
|
+
const DELETE_MEMBER_PATH = `${API_V1}/org/user/{userUuid}`;
|
|
18
|
+
|
|
19
|
+
const membersList = defineOperation({
|
|
20
|
+
id: 'users.members.list',
|
|
21
|
+
summary: 'List organization members (one page)',
|
|
22
|
+
http: { method: 'GET', path: MEMBERS_PATH },
|
|
23
|
+
authorization: { safetyImpact: 'read' },
|
|
24
|
+
sensitivity: 'pii.email',
|
|
25
|
+
mcp: {
|
|
26
|
+
toolName: 'list_organization_members',
|
|
27
|
+
annotations: READ_ONLY_DEFAULT,
|
|
28
|
+
taskSupport: { exposed: false, taskEligible: false },
|
|
29
|
+
},
|
|
30
|
+
cli: { commandPath: 'users list' },
|
|
31
|
+
profiles: [PROFILE_DISCOVERY],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const membersGet = defineOperation({
|
|
35
|
+
id: 'users.members.get',
|
|
36
|
+
summary: 'Get an organization member by UUID',
|
|
37
|
+
http: { method: 'GET', path: MEMBER_PATH },
|
|
38
|
+
authorization: { safetyImpact: 'read' },
|
|
39
|
+
sensitivity: 'pii.email',
|
|
40
|
+
mcp: {
|
|
41
|
+
toolName: 'get_member',
|
|
42
|
+
annotations: READ_ONLY_DEFAULT,
|
|
43
|
+
taskSupport: { exposed: false, taskEligible: false },
|
|
44
|
+
},
|
|
45
|
+
cli: { commandPath: 'users get' },
|
|
46
|
+
profiles: [PROFILE_DISCOVERY],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const membersDelete = defineOperation({
|
|
50
|
+
id: 'users.members.delete',
|
|
51
|
+
summary: 'Permanently remove a user from the organization (irrecoverable; client-only)',
|
|
52
|
+
http: { method: 'DELETE', path: DELETE_MEMBER_PATH },
|
|
53
|
+
authorization: { safetyImpact: 'write-destructive' },
|
|
54
|
+
sensitivity: 'none',
|
|
55
|
+
agentExposure: 'client-only',
|
|
56
|
+
bannedMcpToolName: 'delete_member',
|
|
57
|
+
profiles: [PROFILE_CORE_LIFECYCLE],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/** Organization member operations registered in the shared operation catalog. */
|
|
61
|
+
export const USER_OPERATIONS: readonly OperationDescriptor[] = [
|
|
62
|
+
membersList,
|
|
63
|
+
membersGet,
|
|
64
|
+
membersDelete,
|
|
65
|
+
];
|
package/src/safety.test.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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.
|
|
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
|
});
|