@jupiterone/jupiterone-mcp 2.6.2 → 2.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.
- package/README.md +122 -72
- package/dist/client/graphql/mutations.d.ts +16 -0
- package/dist/client/graphql/mutations.d.ts.map +1 -1
- package/dist/client/graphql/mutations.js +126 -1
- package/dist/client/graphql/mutations.js.map +1 -1
- package/dist/client/graphql/queries.d.ts +27 -2
- package/dist/client/graphql/queries.d.ts.map +1 -1
- package/dist/client/graphql/queries.js +96 -4
- package/dist/client/graphql/queries.js.map +1 -1
- package/dist/client/jupiterone-client.d.ts +30 -0
- package/dist/client/jupiterone-client.d.ts.map +1 -1
- package/dist/client/jupiterone-client.js +60 -0
- package/dist/client/jupiterone-client.js.map +1 -1
- package/dist/client/services/compliance-service.d.ts +48 -1
- package/dist/client/services/compliance-service.d.ts.map +1 -1
- package/dist/client/services/compliance-service.js +292 -0
- package/dist/client/services/compliance-service.js.map +1 -1
- package/dist/client/services/j1ql-service.d.ts +10 -2
- package/dist/client/services/j1ql-service.d.ts.map +1 -1
- package/dist/client/services/j1ql-service.js +11 -3
- package/dist/client/services/j1ql-service.js.map +1 -1
- package/dist/client/services/rule-service.d.ts +3 -1
- package/dist/client/services/rule-service.d.ts.map +1 -1
- package/dist/client/services/rule-service.js +7 -5
- package/dist/client/services/rule-service.js.map +1 -1
- package/dist/generated/description-map.d.ts.map +1 -1
- package/dist/generated/description-map.js +692 -38
- package/dist/generated/description-map.js.map +1 -1
- package/dist/server/mcp-server.d.ts.map +1 -1
- package/dist/server/mcp-server.js +38 -1
- package/dist/server/mcp-server.js.map +1 -1
- package/dist/server/skills.d.ts +25 -0
- package/dist/server/skills.d.ts.map +1 -0
- package/dist/server/skills.js +115 -0
- package/dist/server/skills.js.map +1 -0
- package/dist/server/tools/compliance/attestations.d.ts.map +1 -1
- package/dist/server/tools/compliance/attestations.js +20 -37
- package/dist/server/tools/compliance/attestations.js.map +1 -1
- package/dist/server/tools/compliance/authoring.d.ts +3 -0
- package/dist/server/tools/compliance/authoring.d.ts.map +1 -0
- package/dist/server/tools/compliance/authoring.js +773 -0
- package/dist/server/tools/compliance/authoring.js.map +1 -0
- package/dist/server/tools/compliance/controls.d.ts.map +1 -1
- package/dist/server/tools/compliance/controls.js +193 -19
- package/dist/server/tools/compliance/controls.js.map +1 -1
- package/dist/server/tools/compliance/exemptions.d.ts +3 -0
- package/dist/server/tools/compliance/exemptions.d.ts.map +1 -0
- package/dist/server/tools/compliance/exemptions.js +287 -0
- package/dist/server/tools/compliance/exemptions.js.map +1 -0
- package/dist/server/tools/compliance/framework.d.ts +3 -0
- package/dist/server/tools/compliance/framework.d.ts.map +1 -0
- package/dist/server/tools/compliance/framework.js +445 -0
- package/dist/server/tools/compliance/framework.js.map +1 -0
- package/dist/server/tools/compliance/graph.d.ts +56 -0
- package/dist/server/tools/compliance/graph.d.ts.map +1 -0
- package/dist/server/tools/compliance/graph.js +115 -0
- package/dist/server/tools/compliance/graph.js.map +1 -0
- package/dist/server/tools/compliance/index.d.ts +2 -16
- package/dist/server/tools/compliance/index.d.ts.map +1 -1
- package/dist/server/tools/compliance/index.js +45 -15
- package/dist/server/tools/compliance/index.js.map +1 -1
- package/dist/server/tools/compliance/requirements.d.ts +3 -0
- package/dist/server/tools/compliance/requirements.d.ts.map +1 -0
- package/dist/server/tools/compliance/requirements.js +243 -0
- package/dist/server/tools/compliance/requirements.js.map +1 -0
- package/dist/server/tools/compliance/schemas.d.ts +409 -8
- package/dist/server/tools/compliance/schemas.d.ts.map +1 -1
- package/dist/server/tools/compliance/schemas.js +337 -7
- package/dist/server/tools/compliance/schemas.js.map +1 -1
- package/dist/server/tools/compliance/shared.d.ts +103 -1
- package/dist/server/tools/compliance/shared.d.ts.map +1 -1
- package/dist/server/tools/compliance/shared.js +235 -1
- package/dist/server/tools/compliance/shared.js.map +1 -1
- package/dist/server/tools/compliance/summary.d.ts +2 -8
- package/dist/server/tools/compliance/summary.d.ts.map +1 -1
- package/dist/server/tools/compliance/summary.js +149 -87
- package/dist/server/tools/compliance/summary.js.map +1 -1
- package/dist/server/tools/compliance/tests.d.ts +3 -0
- package/dist/server/tools/compliance/tests.d.ts.map +1 -0
- package/dist/server/tools/compliance/tests.js +463 -0
- package/dist/server/tools/compliance/tests.js.map +1 -0
- package/dist/server/tools/compliance/transitions.d.ts +6 -0
- package/dist/server/tools/compliance/transitions.d.ts.map +1 -0
- package/dist/server/tools/compliance/transitions.js +659 -0
- package/dist/server/tools/compliance/transitions.js.map +1 -0
- package/dist/server/tools/integrations.d.ts.map +1 -1
- package/dist/server/tools/integrations.js +43 -49
- package/dist/server/tools/integrations.js.map +1 -1
- package/dist/server/tools/reference.d.ts +1 -1
- package/dist/server/tools/reference.d.ts.map +1 -1
- package/dist/server/tools/reference.js +1 -0
- package/dist/server/tools/reference.js.map +1 -1
- package/dist/server/tools/result.d.ts +3 -2
- package/dist/server/tools/result.d.ts.map +1 -1
- package/dist/server/tools/result.js +3 -2
- package/dist/server/tools/result.js.map +1 -1
- package/dist/server/tools/rules.d.ts.map +1 -1
- package/dist/server/tools/rules.js +19 -21
- package/dist/server/tools/rules.js.map +1 -1
- package/dist/server/tools/types.d.ts +2 -0
- package/dist/server/tools/types.d.ts.map +1 -1
- package/dist/server/tools/types.js.map +1 -1
- package/dist/server/tools/usage.d.ts +6 -7
- package/dist/server/tools/usage.d.ts.map +1 -1
- package/dist/server/tools/usage.js +193 -4
- package/dist/server/tools/usage.js.map +1 -1
- package/dist/types/jupiterone.d.ts +179 -0
- package/dist/types/jupiterone.d.ts.map +1 -1
- package/dist/utils/j1ql-validator.d.ts.map +1 -1
- package/dist/utils/j1ql-validator.js +129 -1
- package/dist/utils/j1ql-validator.js.map +1 -1
- package/node_modules/dtrace-provider/build/Makefile +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,773 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.controlAuthoringTools = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const load_description_js_1 = require("../../../utils/load-description.js");
|
|
6
|
+
const result_js_1 = require("../result.js");
|
|
7
|
+
const types_js_1 = require("../types.js");
|
|
8
|
+
const graph_js_1 = require("./graph.js");
|
|
9
|
+
const schemas_js_1 = require("./schemas.js");
|
|
10
|
+
const shared_js_1 = require("./shared.js");
|
|
11
|
+
/**
|
|
12
|
+
* create-controls, link-controls, update-control and delete-control: the control layer of
|
|
13
|
+
* incremental authoring. Controls land DRAFT; one control can serve many requirements, so reuse
|
|
14
|
+
* is a link, not a copy; and the only delete is the narrow one for a draft created by mistake.
|
|
15
|
+
*/
|
|
16
|
+
const MITRE_TECHNIQUE = /^T\d{4}(\.\d{3})?$/;
|
|
17
|
+
const controlIdInput = () => zod_1.z
|
|
18
|
+
.string()
|
|
19
|
+
.trim()
|
|
20
|
+
.min(1)
|
|
21
|
+
.refine((value) => !shared_js_1.CONTROL_ID_FORBIDDEN.test(value), {
|
|
22
|
+
message: 'A control id never contains a quote or a backslash.',
|
|
23
|
+
});
|
|
24
|
+
const requirementIdInput = () => zod_1.z
|
|
25
|
+
.string()
|
|
26
|
+
.trim()
|
|
27
|
+
.min(1)
|
|
28
|
+
.describe('The requirement `id`, from `create-requirements` or `get-compliance-summary` with ' +
|
|
29
|
+
'`includeRequirements`.');
|
|
30
|
+
const mitreInput = () => zod_1.z
|
|
31
|
+
.string()
|
|
32
|
+
.trim()
|
|
33
|
+
.regex(MITRE_TECHNIQUE, { message: 'A MITRE ATT&CK technique id, e.g. T1078 or T1059.001.' });
|
|
34
|
+
/** The requirement a batch writes under, from the graph: its title and the frameworks it serves. */
|
|
35
|
+
function requirementQuery(requirementId) {
|
|
36
|
+
return (`FIND ccm_requirement WITH id = '${requirementId}' AS r ` +
|
|
37
|
+
'RETURN r.id AS id, r.title AS title, r.frameworkId AS frameworkId, ' +
|
|
38
|
+
'r.frameworkIds AS frameworkIds LIMIT 1');
|
|
39
|
+
}
|
|
40
|
+
/** The controls a requirement already has. */
|
|
41
|
+
function linkedControlsQuery(requirementId) {
|
|
42
|
+
return (`FIND ccm_requirement WITH id = '${requirementId}' THAT HAS ccm_control AS c ` +
|
|
43
|
+
`RETURN c.id AS controlId LIMIT ${graph_js_1.GRAPH_PAGE_SIZE}`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Non-deleted controls whose name equals one of `names`, case-folded, anywhere in the account:
|
|
47
|
+
* the backend creates no dedup, and one control can serve many requirements, so a second copy
|
|
48
|
+
* under another requirement splits its evidence.
|
|
49
|
+
*/
|
|
50
|
+
function controlsByNameQuery(names) {
|
|
51
|
+
// Unanchored: the parser refuses regex anchors ("use the ^= query operator"), so the read is a
|
|
52
|
+
// superset and the fold-compared map keeps only exact names.
|
|
53
|
+
const clauses = names.map((name) => `name = /${(0, shared_js_1.j1qlRegex)(name)}/i`).join(' OR ');
|
|
54
|
+
return (`FIND ccm_control WITH ${clauses} AS c ` +
|
|
55
|
+
'RETURN c.id AS id, c.name AS name, c.state AS state, c.frameworkId AS frameworkIds ' +
|
|
56
|
+
`LIMIT ${graph_js_1.GRAPH_PAGE_SIZE}`);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* What to do with a control delete-control keeps. Only a LIVE control can go to RETIRED:
|
|
60
|
+
* transition-controls refuses DRAFT or REVIEW to RETIRED as `passes_through_live`, and following
|
|
61
|
+
* the old "retire it instead" for one would have made a mistaken control LIVE first.
|
|
62
|
+
*/
|
|
63
|
+
function keepInstead(state) {
|
|
64
|
+
if (state === 'LIVE')
|
|
65
|
+
return 'Retire it with `transition-controls` to RETIRED instead.';
|
|
66
|
+
if (state === 'REVIEW') {
|
|
67
|
+
return 'Move it back with `transition-controls` to DRAFT, then delete it if it was never LIVE.';
|
|
68
|
+
}
|
|
69
|
+
if (state === 'RETIRED')
|
|
70
|
+
return 'It is already RETIRED and out of scoring; nothing more is needed.';
|
|
71
|
+
return ('Leave it as a DRAFT, which is never scored, or detach it from its requirements with ' +
|
|
72
|
+
'`update-control` `requirementIds.remove`.');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A control the graph does not list, read from the API: the graph lags a create by up to a
|
|
76
|
+
* minute, so `link-controls` called a control created seconds earlier `not_found`, which invites
|
|
77
|
+
* creating it again (PR #59 review, 2026-10-29).
|
|
78
|
+
*/
|
|
79
|
+
async function confirmControlExists(client, id, deadlineAt) {
|
|
80
|
+
try {
|
|
81
|
+
const read = await (0, shared_js_1.withDeadline)(client.getControlState(id), deadlineAt);
|
|
82
|
+
if (read === shared_js_1.DEADLINE_PASSED)
|
|
83
|
+
return { kind: 'timeout' };
|
|
84
|
+
if (read === null)
|
|
85
|
+
return { kind: 'not_found' };
|
|
86
|
+
return { kind: 'found', name: read.name, state: read.state };
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if ((0, shared_js_1.isAborted)(error))
|
|
90
|
+
throw error;
|
|
91
|
+
if ((0, shared_js_1.isNotFound)(error))
|
|
92
|
+
return { kind: 'not_found' };
|
|
93
|
+
return { kind: 'unread', reason: (0, shared_js_1.backendReason)(error) };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Non-deleted controls by id with their test count, for a link. */
|
|
97
|
+
function controlsByIdQuery(ids) {
|
|
98
|
+
const list = ids.map(shared_js_1.j1qlLiteral).join(' OR ');
|
|
99
|
+
return (`FIND ccm_control WITH id = (${list}) AS c (THAT DEFINES ccm_control_test AS t)? ` +
|
|
100
|
+
`RETURN c.id AS id, c.name AS name, c.state AS state, t.id AS testId LIMIT ${graph_js_1.GRAPH_PAGE_SIZE}`);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Which of `ids` CCM's graph does not list yet. CCM's updateControl looks each added requirement
|
|
104
|
+
* up in the graph and drops any it does not find, with no error (apps
|
|
105
|
+
* `controlRelationshipUtils.ts`; on j1dev 2026-10-30 an add of an unknown id answered success and
|
|
106
|
+
* wrote nothing), so a link to a requirement created moments ago could be reported `linked` while
|
|
107
|
+
* no edge exists.
|
|
108
|
+
*/
|
|
109
|
+
async function requirementsNotInGraph(client, ids, deadlineAt) {
|
|
110
|
+
const match = ids.length === 1 ? (0, shared_js_1.j1qlLiteral)(ids[0]) : `(${ids.map(shared_js_1.j1qlLiteral).join(' OR ')})`;
|
|
111
|
+
const query = `FIND ccm_requirement WITH id = ${match} AS r RETURN r.id AS id LIMIT ${graph_js_1.GRAPH_PAGE_SIZE}`;
|
|
112
|
+
const read = await (0, graph_js_1.runGraphQuery)(client, query, deadlineAt, 1, { fresh: true });
|
|
113
|
+
if (read.kind !== 'rows') {
|
|
114
|
+
if (read.kind === 'failed')
|
|
115
|
+
console.error('Error reading requirements from the graph:', read.error);
|
|
116
|
+
return 'unreadable';
|
|
117
|
+
}
|
|
118
|
+
const listed = new Set(read.rows.map((row) => (0, graph_js_1.rowText)(row.id)));
|
|
119
|
+
return ids.filter((id) => !listed.has(id));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The refusal for requirement ids an update would add that CCM would silently drop: ids nothing
|
|
123
|
+
* holds are `requirement_not_found`, ids the API holds but the graph does not list yet are
|
|
124
|
+
* `not_ready`. Null when every id is listed.
|
|
125
|
+
*/
|
|
126
|
+
async function requirementsToAddRefusal(client, ids, deadlineAt, timings) {
|
|
127
|
+
const missing = await (0, shared_js_1.timed)(timings, 'indexed', () => requirementsNotInGraph(client, ids, deadlineAt));
|
|
128
|
+
if (missing === 'unreadable')
|
|
129
|
+
return (0, shared_js_1.readFailed)('The requirements to add', { ids });
|
|
130
|
+
if (missing.length === 0)
|
|
131
|
+
return null;
|
|
132
|
+
const reads = await Promise.all(missing.map((id) => readRequirement(client, id, deadlineAt)));
|
|
133
|
+
if (reads.includes('unreadable'))
|
|
134
|
+
return (0, shared_js_1.readFailed)('The requirements to add', { ids });
|
|
135
|
+
const unknown = missing.filter((_, index) => reads[index] === null);
|
|
136
|
+
if (unknown.length > 0) {
|
|
137
|
+
return (0, shared_js_1.refusal)('requirement_not_found', `Control not updated — no requirement has the id${unknown.length === 1 ? '' : 's'} ` +
|
|
138
|
+
`${unknown.join(', ')}. Take ids from \`create-requirements\` or from ` +
|
|
139
|
+
'`get-compliance-summary` with `includeRequirements`.', { requirementIds: unknown });
|
|
140
|
+
}
|
|
141
|
+
return requirementNotReady(missing);
|
|
142
|
+
}
|
|
143
|
+
const requirementNotReady = (requirementIds) => (0, shared_js_1.refusal)('not_ready', 'Nothing was linked — CCM cannot yet see the requirement it would link to, and it drops such ' +
|
|
144
|
+
'a link without an error. A requirement created moments ago appears within about a ' +
|
|
145
|
+
'minute: call again then with the same input.', { requirementIds });
|
|
146
|
+
/**
|
|
147
|
+
* The requirement as CCM holds it now, from the API: the graph lags a create by seconds to
|
|
148
|
+
* minutes, and a batch that read it there refused a requirement created moments earlier as
|
|
149
|
+
* `requirement_not_found` (j1dev, 2026-10-28). The graph answers only when the API read fails.
|
|
150
|
+
*/
|
|
151
|
+
async function readRequirement(client, requirementId, deadlineAt) {
|
|
152
|
+
try {
|
|
153
|
+
const fresh = await (0, shared_js_1.withDeadline)(client.getRequirement(requirementId), deadlineAt);
|
|
154
|
+
if (fresh === shared_js_1.DEADLINE_PASSED)
|
|
155
|
+
return 'unreadable';
|
|
156
|
+
if (fresh === null)
|
|
157
|
+
return null;
|
|
158
|
+
return { id: fresh.id, title: fresh.title, frameworkIds: fresh.frameworkIds ?? [] };
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if ((0, shared_js_1.isAborted)(error))
|
|
162
|
+
throw error;
|
|
163
|
+
if ((0, shared_js_1.isNotFound)(error))
|
|
164
|
+
return null;
|
|
165
|
+
console.error('Error reading a requirement from the API; trying the graph:', error);
|
|
166
|
+
}
|
|
167
|
+
const read = await (0, graph_js_1.runGraphQuery)(client, requirementQuery(requirementId), deadlineAt, 1, {
|
|
168
|
+
fresh: true,
|
|
169
|
+
});
|
|
170
|
+
if (read.kind !== 'rows') {
|
|
171
|
+
if (read.kind === 'failed')
|
|
172
|
+
console.error('Error reading a requirement:', read.error);
|
|
173
|
+
return 'unreadable';
|
|
174
|
+
}
|
|
175
|
+
const row = read.rows[0];
|
|
176
|
+
const id = row ? (0, graph_js_1.rowText)(row.id) : null;
|
|
177
|
+
if (!id)
|
|
178
|
+
return null;
|
|
179
|
+
const frameworkIds = [...new Set([...(0, graph_js_1.rowIds)(row.frameworkIds), ...(0, graph_js_1.rowIds)(row.frameworkId)])];
|
|
180
|
+
return { id, title: (0, graph_js_1.rowText)(row.title), frameworkIds };
|
|
181
|
+
}
|
|
182
|
+
const requirementNotFound = (requirementId) => (0, shared_js_1.refusal)('requirement_not_found', 'No requirement has this id. Take the `id` from `create-requirements` or from ' +
|
|
183
|
+
'`get-compliance-summary` with `includeRequirements`.', { requirementId });
|
|
184
|
+
function existingByName(read) {
|
|
185
|
+
if (read.kind !== 'rows')
|
|
186
|
+
return null;
|
|
187
|
+
const byName = new Map();
|
|
188
|
+
for (const row of read.rows) {
|
|
189
|
+
const id = (0, graph_js_1.rowText)(row.id);
|
|
190
|
+
const name = (0, graph_js_1.rowText)(row.name);
|
|
191
|
+
if (!id || !name)
|
|
192
|
+
continue;
|
|
193
|
+
byName.set((0, shared_js_1.foldName)(name), {
|
|
194
|
+
id,
|
|
195
|
+
name,
|
|
196
|
+
state: (0, graph_js_1.rowText)(row.state),
|
|
197
|
+
frameworkIds: (0, graph_js_1.rowIds)(row.frameworkIds),
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return byName;
|
|
201
|
+
}
|
|
202
|
+
const NOTE_CONTROLS_NEXT = 'Confirm the list with the user, then `create-control-test` for each automated control; ' +
|
|
203
|
+
'attestation-only controls need no test and are attested once LIVE.';
|
|
204
|
+
/**
|
|
205
|
+
* What to do about the controls a call did not finish. The duplicate check reads the graph, which
|
|
206
|
+
* lags a create by up to a minute, so resending the whole batch at once re-creates what this call
|
|
207
|
+
* created; the hand-back holds only the items never attempted (PR #59 review, 2026-10-29). An item
|
|
208
|
+
* cut off mid-create may have landed and is read back first.
|
|
209
|
+
*/
|
|
210
|
+
function remainingNextStep(notAttempted, interrupted) {
|
|
211
|
+
const parts = [];
|
|
212
|
+
if (notAttempted > 0) {
|
|
213
|
+
parts.push(`${notAttempted} control${notAttempted === 1 ? ' was' : 's were'} not attempted in this ` +
|
|
214
|
+
"call's budget: call again with `nextInputs`, which holds only those items. Never resend " +
|
|
215
|
+
'the items created here: the duplicate check reads the graph, which lags a create by up ' +
|
|
216
|
+
'to a minute, so a resend makes a second copy.');
|
|
217
|
+
}
|
|
218
|
+
if (interrupted > 0) {
|
|
219
|
+
parts.push(`${interrupted} ${interrupted === 1 ? 'was' : 'were'} cut off mid-create and may have ` +
|
|
220
|
+
'landed: wait a minute, then look them up with `list-controls` `search` before creating ' +
|
|
221
|
+
'them again.');
|
|
222
|
+
}
|
|
223
|
+
return parts.join(' ');
|
|
224
|
+
}
|
|
225
|
+
/** The requirement's first framework, named and linked, when the frameworks could be read. */
|
|
226
|
+
async function frameworkOf(requirement, frameworksRead, subdomain, deadlineAt) {
|
|
227
|
+
const read = await (0, shared_js_1.withDeadline)(frameworksRead.catch((error) => {
|
|
228
|
+
console.error('Error reading the frameworks of a requirement:', error);
|
|
229
|
+
return { frameworks: [] };
|
|
230
|
+
}), deadlineAt);
|
|
231
|
+
const names = read === shared_js_1.DEADLINE_PASSED
|
|
232
|
+
? new Map()
|
|
233
|
+
: (0, shared_js_1.frameworkNameMap)(read.frameworks);
|
|
234
|
+
const [first] = requirement.frameworkIds;
|
|
235
|
+
return first
|
|
236
|
+
? { id: first, name: names.get(first) ?? null, url: (0, shared_js_1.frameworkUrl)(first, subdomain) }
|
|
237
|
+
: null;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* The batch with each name once (`unique`), the slot in `unique` every input maps to (`slotOf`),
|
|
241
|
+
* and which inputs are later copies of an earlier one (`isCopy`). A copy check that compared an
|
|
242
|
+
* input's slot with its own position took the item after a duplicate for a copy of itself, and
|
|
243
|
+
* reported a control it had just created as `already_exists`.
|
|
244
|
+
*/
|
|
245
|
+
function uniqueByName(items) {
|
|
246
|
+
const seen = new Map();
|
|
247
|
+
const unique = [];
|
|
248
|
+
const slotOf = [];
|
|
249
|
+
const isCopy = [];
|
|
250
|
+
items.forEach((item) => {
|
|
251
|
+
const key = (0, shared_js_1.foldName)(item.name);
|
|
252
|
+
const first = seen.get(key);
|
|
253
|
+
if (first === undefined) {
|
|
254
|
+
seen.set(key, unique.length);
|
|
255
|
+
slotOf.push(unique.length);
|
|
256
|
+
isCopy.push(false);
|
|
257
|
+
unique.push(item);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
slotOf.push(first);
|
|
261
|
+
isCopy.push(true);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
return { unique, slotOf, isCopy };
|
|
265
|
+
}
|
|
266
|
+
function projectWritten(control, subdomain) {
|
|
267
|
+
return {
|
|
268
|
+
id: control.id,
|
|
269
|
+
name: control.name,
|
|
270
|
+
state: control.state,
|
|
271
|
+
status: control.status,
|
|
272
|
+
identifier: control.identifier,
|
|
273
|
+
frameworkIds: control.frameworkIds,
|
|
274
|
+
url: (0, shared_js_1.controlUrl)(control.id, subdomain),
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
exports.controlAuthoringTools = [
|
|
278
|
+
(0, types_js_1.defineTool)({
|
|
279
|
+
name: 'create-controls',
|
|
280
|
+
title: 'Create Controls',
|
|
281
|
+
description: (0, load_description_js_1.loadDescription)('create-controls.md'),
|
|
282
|
+
inputSchema: {
|
|
283
|
+
requirementId: requirementIdInput(),
|
|
284
|
+
controls: zod_1.z
|
|
285
|
+
.array(zod_1.z.object({
|
|
286
|
+
name: zod_1.z.string().trim().min(1).describe("The control's name, in the customer's words."),
|
|
287
|
+
description: zod_1.z
|
|
288
|
+
.string()
|
|
289
|
+
.trim()
|
|
290
|
+
.min(1)
|
|
291
|
+
.optional()
|
|
292
|
+
.describe('What the organisation does, naming the tool or process.'),
|
|
293
|
+
identifier: zod_1.z
|
|
294
|
+
.string()
|
|
295
|
+
.trim()
|
|
296
|
+
.min(1)
|
|
297
|
+
.optional()
|
|
298
|
+
.describe("The customer's own id, verbatim."),
|
|
299
|
+
catalog: zod_1.z.string().trim().min(1).optional(),
|
|
300
|
+
owner: (0, shared_js_1.ownerEmail)().optional().describe("The owner's email address."),
|
|
301
|
+
remediation: zod_1.z.string().trim().min(1).optional().describe('Markdown.'),
|
|
302
|
+
exceptionProcess: zod_1.z.string().trim().min(1).optional().describe('Markdown.'),
|
|
303
|
+
mitreTechnique: mitreInput().optional(),
|
|
304
|
+
state: zod_1.z
|
|
305
|
+
.enum(['DRAFT', 'LIVE'])
|
|
306
|
+
.optional()
|
|
307
|
+
.describe('DRAFT (default) or LIVE, which counts against the live-controls limit at once.'),
|
|
308
|
+
}))
|
|
309
|
+
.min(1)
|
|
310
|
+
.max(shared_js_1.MAX_CONTROLS_PER_CALL)
|
|
311
|
+
.describe(`The controls to create under the requirement, 1 to ${shared_js_1.MAX_CONTROLS_PER_CALL}.`),
|
|
312
|
+
},
|
|
313
|
+
outputSchema: schemas_js_1.createControlsOutput,
|
|
314
|
+
annotations: types_js_1.CREATE,
|
|
315
|
+
accessDeniedHint: shared_js_1.ACCESS_DENIED_HINT,
|
|
316
|
+
errorContext: 'Error creating controls',
|
|
317
|
+
handler: async ({ requirementId, controls }, client, validator) => {
|
|
318
|
+
if (!graph_js_1.GRAPH_REQUIREMENT_ID.test(requirementId))
|
|
319
|
+
return requirementNotFound(requirementId);
|
|
320
|
+
const startedAt = Date.now();
|
|
321
|
+
const readDeadline = startedAt + shared_js_1.AUTHORING_READ_DEADLINE_MS;
|
|
322
|
+
const writeDeadline = startedAt + shared_js_1.AUTHORING_DEADLINE_MS;
|
|
323
|
+
const timings = {};
|
|
324
|
+
const { unique, slotOf, isCopy } = uniqueByName(controls);
|
|
325
|
+
const dedupQuery = controlsByNameQuery(unique.map((item) => item.name));
|
|
326
|
+
if (!validator.validateQuery(dedupQuery).isValid) {
|
|
327
|
+
return (0, shared_js_1.validationError)('A control name holds characters the duplicate check cannot match; use plain words.');
|
|
328
|
+
}
|
|
329
|
+
const subdomainRead = (0, shared_js_1.readSubdomain)(client, timings);
|
|
330
|
+
const frameworksRead = client.listAllControlFrameworks();
|
|
331
|
+
frameworksRead.catch(() => undefined);
|
|
332
|
+
const [requirement, existingRead] = await Promise.all([
|
|
333
|
+
(0, shared_js_1.timed)(timings, 'requirement', () => readRequirement(client, requirementId, readDeadline)),
|
|
334
|
+
(0, shared_js_1.timed)(timings, 'dedup', () => (0, graph_js_1.runGraphQuery)(client, dedupQuery, readDeadline, graph_js_1.GRAPH_MAX_PAGES, { fresh: true })),
|
|
335
|
+
]);
|
|
336
|
+
if (requirement === 'unreadable')
|
|
337
|
+
return (0, shared_js_1.readFailed)('The requirement', { requirementId });
|
|
338
|
+
if (!requirement)
|
|
339
|
+
return requirementNotFound(requirementId);
|
|
340
|
+
const existing = existingByName(existingRead);
|
|
341
|
+
if (!existing)
|
|
342
|
+
return (0, shared_js_1.readFailed)("The account's existing controls", { requirementId });
|
|
343
|
+
// The name match is a case-insensitive substring read, so a common word can match more
|
|
344
|
+
// controls than the read holds; an exact name past the cut would slip through the
|
|
345
|
+
// duplicate check (PR #59 review).
|
|
346
|
+
if (existingRead.kind === 'rows' && existingRead.more) {
|
|
347
|
+
return (0, shared_js_1.refusal)('read_failed', 'More existing controls match these names than one call reads, so the duplicate ' +
|
|
348
|
+
'check would be incomplete and nothing was created. Use more distinctive control ' +
|
|
349
|
+
'names, or create the common-word ones in a batch of their own.', { requirementId });
|
|
350
|
+
}
|
|
351
|
+
const subdomain = await (0, shared_js_1.subdomainBy)(subdomainRead, readDeadline);
|
|
352
|
+
const framework = await frameworkOf(requirement, frameworksRead, subdomain, readDeadline);
|
|
353
|
+
const names = new Map(framework?.name ? [[framework.id, framework.name]] : []);
|
|
354
|
+
const slots = await (0, shared_js_1.timed)(timings, 'create', () => (0, shared_js_1.runBatch)(unique, {
|
|
355
|
+
deadlineAt: writeDeadline,
|
|
356
|
+
concurrency: shared_js_1.BATCH_CONCURRENCY,
|
|
357
|
+
stopAfter: (value) => value.outcome === 'refused' && value.liveLimit === true,
|
|
358
|
+
}, async (item) => {
|
|
359
|
+
const match = existing.get((0, shared_js_1.foldName)(item.name));
|
|
360
|
+
if (match) {
|
|
361
|
+
return {
|
|
362
|
+
outcome: 'already_exists',
|
|
363
|
+
name: item.name,
|
|
364
|
+
control: {
|
|
365
|
+
id: match.id,
|
|
366
|
+
name: match.name,
|
|
367
|
+
state: match.state,
|
|
368
|
+
frameworkNames: match.frameworkIds.map((id) => names.get(id) ?? null),
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
const input = {
|
|
373
|
+
name: item.name,
|
|
374
|
+
description: item.description,
|
|
375
|
+
requirementIds: [requirementId],
|
|
376
|
+
state: item.state,
|
|
377
|
+
identifier: item.identifier,
|
|
378
|
+
catalog: item.catalog,
|
|
379
|
+
owner: item.owner,
|
|
380
|
+
remediation: item.remediation,
|
|
381
|
+
exceptionProcess: item.exceptionProcess,
|
|
382
|
+
mitreTechnique: item.mitreTechnique,
|
|
383
|
+
};
|
|
384
|
+
try {
|
|
385
|
+
const created = await client.createControl(input);
|
|
386
|
+
existing.set((0, shared_js_1.foldName)(item.name), {
|
|
387
|
+
id: created.id,
|
|
388
|
+
name: created.name,
|
|
389
|
+
state: created.state,
|
|
390
|
+
frameworkIds: created.frameworkIds ?? [],
|
|
391
|
+
});
|
|
392
|
+
return {
|
|
393
|
+
outcome: 'created',
|
|
394
|
+
id: created.id,
|
|
395
|
+
name: created.name,
|
|
396
|
+
identifier: created.identifier,
|
|
397
|
+
state: created.state,
|
|
398
|
+
url: (0, shared_js_1.controlUrl)(created.id, subdomain),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
catch (error) {
|
|
402
|
+
if ((0, shared_js_1.isAborted)(error))
|
|
403
|
+
throw error;
|
|
404
|
+
const limit = (0, shared_js_1.liveLimitReason)(error);
|
|
405
|
+
return limit
|
|
406
|
+
? { outcome: 'refused', name: item.name, reason: limit, liveLimit: true }
|
|
407
|
+
: { outcome: 'refused', name: item.name, reason: (0, shared_js_1.backendReason)(error) };
|
|
408
|
+
}
|
|
409
|
+
}));
|
|
410
|
+
const items = controls.map((item, index) => {
|
|
411
|
+
const slot = slots[slotOf[index]];
|
|
412
|
+
if (slot.kind !== 'done')
|
|
413
|
+
return { outcome: slot.kind, name: item.name };
|
|
414
|
+
const leader = slot.value;
|
|
415
|
+
// A later copy of a created name in the same batch already exists once the first landed.
|
|
416
|
+
return leader.outcome === 'created' && isCopy[index]
|
|
417
|
+
? {
|
|
418
|
+
outcome: 'already_exists',
|
|
419
|
+
name: item.name,
|
|
420
|
+
control: {
|
|
421
|
+
id: leader.id,
|
|
422
|
+
name: leader.name,
|
|
423
|
+
state: leader.state,
|
|
424
|
+
frameworkNames: [],
|
|
425
|
+
},
|
|
426
|
+
}
|
|
427
|
+
: { ...leader, name: item.name };
|
|
428
|
+
});
|
|
429
|
+
const count = (outcome) => items.filter((item) => item.outcome === outcome).length;
|
|
430
|
+
const liveLimitHit = items.some((item) => 'liveLimit' in item && item.liveLimit === true);
|
|
431
|
+
const remaining = count('not_attempted') + count('interrupted');
|
|
432
|
+
const notAttempted = controls.filter((_item, index) => !isCopy[index] && slots[slotOf[index]].kind === 'not_attempted');
|
|
433
|
+
return (0, result_js_1.structuredResult)({
|
|
434
|
+
outcome: 'ok',
|
|
435
|
+
requirement: { id: requirement.id, title: requirement.title },
|
|
436
|
+
framework,
|
|
437
|
+
items,
|
|
438
|
+
createdCount: count('created'),
|
|
439
|
+
existingCount: count('already_exists'),
|
|
440
|
+
refusedCount: count('refused'),
|
|
441
|
+
remaining,
|
|
442
|
+
liveLimitHit,
|
|
443
|
+
...(!liveLimitHit && notAttempted.length > 0
|
|
444
|
+
? { nextInputs: [{ requirementId, controls: notAttempted }] }
|
|
445
|
+
: {}),
|
|
446
|
+
nextStep: liveLimitHit
|
|
447
|
+
? 'The live-controls limit was reached, so later items were not attempted: retire ' +
|
|
448
|
+
'controls or create the rest as DRAFT.'
|
|
449
|
+
: remaining > 0
|
|
450
|
+
? remainingNextStep(count('not_attempted'), count('interrupted'))
|
|
451
|
+
: NOTE_CONTROLS_NEXT,
|
|
452
|
+
timingsMs: timings,
|
|
453
|
+
});
|
|
454
|
+
},
|
|
455
|
+
}),
|
|
456
|
+
(0, types_js_1.defineTool)({
|
|
457
|
+
name: 'link-controls',
|
|
458
|
+
title: 'Link Controls to a Requirement',
|
|
459
|
+
description: (0, load_description_js_1.loadDescription)('link-controls.md'),
|
|
460
|
+
inputSchema: {
|
|
461
|
+
requirementId: requirementIdInput(),
|
|
462
|
+
controlIds: zod_1.z
|
|
463
|
+
.array(controlIdInput())
|
|
464
|
+
.min(1)
|
|
465
|
+
.max(shared_js_1.MAX_CONTROLS_PER_CALL)
|
|
466
|
+
.describe(`Exact control ids to attach, 1 to ${shared_js_1.MAX_CONTROLS_PER_CALL}.`),
|
|
467
|
+
},
|
|
468
|
+
outputSchema: schemas_js_1.linkControlsOutput,
|
|
469
|
+
// Linking is additive and a repeat reports already_linked, so a replay has no further effect.
|
|
470
|
+
annotations: { ...types_js_1.CREATE, idempotentHint: true },
|
|
471
|
+
accessDeniedHint: shared_js_1.ACCESS_DENIED_HINT,
|
|
472
|
+
errorContext: 'Error linking controls',
|
|
473
|
+
handler: async ({ requirementId, controlIds }, client) => {
|
|
474
|
+
if (!graph_js_1.GRAPH_REQUIREMENT_ID.test(requirementId))
|
|
475
|
+
return requirementNotFound(requirementId);
|
|
476
|
+
const startedAt = Date.now();
|
|
477
|
+
const readDeadline = startedAt + shared_js_1.AUTHORING_READ_DEADLINE_MS;
|
|
478
|
+
const writeDeadline = startedAt + shared_js_1.AUTHORING_DEADLINE_MS;
|
|
479
|
+
const timings = {};
|
|
480
|
+
const ids = [...new Set(controlIds)];
|
|
481
|
+
const subdomainRead = (0, shared_js_1.readSubdomain)(client, timings);
|
|
482
|
+
const [requirement, linkedRead, controlsRead, notInGraph] = await Promise.all([
|
|
483
|
+
(0, shared_js_1.timed)(timings, 'requirement', () => readRequirement(client, requirementId, readDeadline)),
|
|
484
|
+
(0, shared_js_1.timed)(timings, 'linked', () => (0, graph_js_1.runGraphQuery)(client, linkedControlsQuery(requirementId), readDeadline, graph_js_1.GRAPH_MAX_PAGES, {
|
|
485
|
+
fresh: true,
|
|
486
|
+
})),
|
|
487
|
+
(0, shared_js_1.timed)(timings, 'controls', () => (0, graph_js_1.runGraphQuery)(client, controlsByIdQuery(ids), readDeadline, 1, { fresh: true })),
|
|
488
|
+
(0, shared_js_1.timed)(timings, 'indexed', () => requirementsNotInGraph(client, [requirementId], readDeadline)),
|
|
489
|
+
]);
|
|
490
|
+
if (requirement === 'unreadable')
|
|
491
|
+
return (0, shared_js_1.readFailed)('The requirement', { requirementId });
|
|
492
|
+
if (!requirement)
|
|
493
|
+
return requirementNotFound(requirementId);
|
|
494
|
+
if (linkedRead.kind !== 'rows' ||
|
|
495
|
+
controlsRead.kind !== 'rows' ||
|
|
496
|
+
notInGraph === 'unreadable') {
|
|
497
|
+
return (0, shared_js_1.readFailed)("The requirement's controls", { requirementId });
|
|
498
|
+
}
|
|
499
|
+
if (notInGraph.length > 0)
|
|
500
|
+
return requirementNotReady(notInGraph);
|
|
501
|
+
const linked = new Set(linkedRead.rows.map((row) => (0, graph_js_1.rowText)(row.controlId)).filter(Boolean));
|
|
502
|
+
const known = new Map();
|
|
503
|
+
for (const row of controlsRead.rows) {
|
|
504
|
+
const id = (0, graph_js_1.rowText)(row.id);
|
|
505
|
+
if (!id)
|
|
506
|
+
continue;
|
|
507
|
+
const entry = known.get(id) ?? {
|
|
508
|
+
name: (0, graph_js_1.rowText)(row.name),
|
|
509
|
+
state: (0, graph_js_1.rowText)(row.state),
|
|
510
|
+
tests: 0,
|
|
511
|
+
};
|
|
512
|
+
if ((0, graph_js_1.rowText)(row.testId))
|
|
513
|
+
entry.tests = (entry.tests ?? 0) + 1;
|
|
514
|
+
known.set(id, entry);
|
|
515
|
+
}
|
|
516
|
+
const subdomain = await (0, shared_js_1.subdomainBy)(subdomainRead, readDeadline);
|
|
517
|
+
const slots = await (0, shared_js_1.timed)(timings, 'link', () => (0, shared_js_1.runBatch)(ids, { deadlineAt: writeDeadline, concurrency: shared_js_1.BATCH_CONCURRENCY }, async (id) => {
|
|
518
|
+
let control = known.get(id);
|
|
519
|
+
if (!control) {
|
|
520
|
+
const read = await confirmControlExists(client, id, writeDeadline);
|
|
521
|
+
if (read.kind === 'not_found')
|
|
522
|
+
return { outcome: 'not_found', id };
|
|
523
|
+
// Out of time is unfinished, not refused: refused left it out of `remaining` and the
|
|
524
|
+
// nextStep suggested creating a control that only missed its read (PR #59 review).
|
|
525
|
+
if (read.kind === 'timeout')
|
|
526
|
+
return { outcome: 'not_attempted', id };
|
|
527
|
+
if (read.kind === 'unread')
|
|
528
|
+
return { outcome: 'refused', id, reason: read.reason };
|
|
529
|
+
control = { name: read.name, state: read.state, tests: null };
|
|
530
|
+
known.set(id, control);
|
|
531
|
+
}
|
|
532
|
+
const found = {
|
|
533
|
+
id,
|
|
534
|
+
name: control.name,
|
|
535
|
+
state: control.state,
|
|
536
|
+
url: (0, shared_js_1.controlUrl)(id, subdomain),
|
|
537
|
+
};
|
|
538
|
+
if (linked.has(id))
|
|
539
|
+
return { outcome: 'already_linked', ...found };
|
|
540
|
+
try {
|
|
541
|
+
await client.updateControl({ id, requirementIds: { add: [requirementId] } });
|
|
542
|
+
return { outcome: 'linked', ...found };
|
|
543
|
+
}
|
|
544
|
+
catch (error) {
|
|
545
|
+
if ((0, shared_js_1.isAborted)(error))
|
|
546
|
+
throw error;
|
|
547
|
+
if ((0, shared_js_1.isNotFound)(error))
|
|
548
|
+
return { outcome: 'not_found', id };
|
|
549
|
+
return { outcome: 'refused', ...found, reason: (0, shared_js_1.backendReason)(error) };
|
|
550
|
+
}
|
|
551
|
+
}));
|
|
552
|
+
const items = slots.map((slot, index) => slot.kind === 'done' ? slot.value : { outcome: slot.kind, id: ids[index] });
|
|
553
|
+
const count = (outcome) => items.filter((item) => item.outcome === outcome).length;
|
|
554
|
+
const notes = [];
|
|
555
|
+
for (const item of items) {
|
|
556
|
+
if (item.outcome !== 'linked' && item.outcome !== 'already_linked')
|
|
557
|
+
continue;
|
|
558
|
+
const control = known.get(item.id);
|
|
559
|
+
if (!control)
|
|
560
|
+
continue;
|
|
561
|
+
if (control.state !== 'LIVE') {
|
|
562
|
+
notes.push(`${item.id} is ${control.state ?? 'in no state'} and counts once promoted.`);
|
|
563
|
+
}
|
|
564
|
+
if (control.tests === 0) {
|
|
565
|
+
notes.push(`${item.id} has no tests and passes only by attestation once LIVE.`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const remaining = count('not_attempted') + count('interrupted');
|
|
569
|
+
return (0, result_js_1.structuredResult)({
|
|
570
|
+
outcome: 'ok',
|
|
571
|
+
requirement: { id: requirement.id, title: requirement.title },
|
|
572
|
+
items,
|
|
573
|
+
linkedCount: count('linked'),
|
|
574
|
+
alreadyLinkedCount: count('already_linked'),
|
|
575
|
+
notFoundCount: count('not_found'),
|
|
576
|
+
refusedCount: count('refused'),
|
|
577
|
+
remaining,
|
|
578
|
+
notes,
|
|
579
|
+
nextStep: remaining > 0
|
|
580
|
+
? `${remaining} control${remaining === 1 ? ' was' : 's were'} not reached in this call's ` +
|
|
581
|
+
'budget: call again with the same input; linked ones come back `already_linked`.'
|
|
582
|
+
: 'Confirm with the user, then `create-controls` for what the requirement still lacks.',
|
|
583
|
+
timingsMs: timings,
|
|
584
|
+
});
|
|
585
|
+
},
|
|
586
|
+
}),
|
|
587
|
+
(0, types_js_1.defineTool)({
|
|
588
|
+
name: 'update-control',
|
|
589
|
+
title: 'Update Control',
|
|
590
|
+
description: (0, load_description_js_1.loadDescription)('update-control.md'),
|
|
591
|
+
inputSchema: {
|
|
592
|
+
controlId: controlIdInput().describe('The exact control id.'),
|
|
593
|
+
controlName: zod_1.z.string().trim().min(1).optional().describe('A new name.'),
|
|
594
|
+
// Not nullable: apps-api reads a null description or resource group as "not provided" and
|
|
595
|
+
// keeps the old value (public/control/serializers.ts), so null was answered "Saved" and
|
|
596
|
+
// changed nothing (PR #59 review, 2026-10-30). The other nullable fields do clear.
|
|
597
|
+
description: zod_1.z.string().trim().min(1).optional().describe('A new description.'),
|
|
598
|
+
identifier: zod_1.z
|
|
599
|
+
.string()
|
|
600
|
+
.trim()
|
|
601
|
+
.nullable()
|
|
602
|
+
.optional()
|
|
603
|
+
.describe("The customer's own id; null clears it."),
|
|
604
|
+
catalog: zod_1.z.string().trim().nullable().optional().describe('null clears it.'),
|
|
605
|
+
owner: (0, shared_js_1.ownerEmail)().nullable().optional().describe("The owner's email; null clears it."),
|
|
606
|
+
remediation: zod_1.z.string().nullable().optional().describe('Markdown; null clears it.'),
|
|
607
|
+
exceptionProcess: zod_1.z.string().nullable().optional().describe('Markdown; null clears it.'),
|
|
608
|
+
mitreTechnique: mitreInput().nullable().optional().describe('null clears it.'),
|
|
609
|
+
resourceGroupId: zod_1.z.string().trim().min(1).optional(),
|
|
610
|
+
requirementIds: zod_1.z
|
|
611
|
+
.object({
|
|
612
|
+
set: zod_1.z.array(zod_1.z.string().trim().min(1)).optional(),
|
|
613
|
+
add: zod_1.z.array(zod_1.z.string().trim().min(1)).optional(),
|
|
614
|
+
remove: zod_1.z.array(zod_1.z.string().trim().min(1)).optional(),
|
|
615
|
+
})
|
|
616
|
+
.optional()
|
|
617
|
+
.describe('Exactly one of `set` (replace the list), `add` or `remove`.'),
|
|
618
|
+
},
|
|
619
|
+
outputSchema: schemas_js_1.updateControlOutput,
|
|
620
|
+
// The same input twice leaves the control as the first call did.
|
|
621
|
+
annotations: { ...types_js_1.MUTATE, idempotentHint: true },
|
|
622
|
+
accessDeniedHint: shared_js_1.ACCESS_DENIED_HINT,
|
|
623
|
+
errorContext: 'Error updating control',
|
|
624
|
+
handler: async ({ controlId, requirementIds, controlName, ...rest }, client) => {
|
|
625
|
+
const startedAt = Date.now();
|
|
626
|
+
const fields = { name: controlName, ...rest };
|
|
627
|
+
const changes = Object.entries(fields).filter(([, value]) => value !== undefined);
|
|
628
|
+
if (changes.length === 0 && !requirementIds) {
|
|
629
|
+
return (0, shared_js_1.validationError)('Control not updated — pass at least one field to change; the lifecycle state belongs ' +
|
|
630
|
+
'to `transition-controls`.');
|
|
631
|
+
}
|
|
632
|
+
if (requirementIds) {
|
|
633
|
+
const keys = Object.entries(requirementIds).filter(([, value]) => value !== undefined);
|
|
634
|
+
if (keys.length !== 1) {
|
|
635
|
+
return (0, shared_js_1.validationError)('Control not updated — `requirementIds` takes exactly one of `set`, `add` and `remove`.');
|
|
636
|
+
}
|
|
637
|
+
if (requirementIds.set?.length === 0) {
|
|
638
|
+
return (0, shared_js_1.validationError)('Control not updated — `set: []` would detach it from every requirement in every ' +
|
|
639
|
+
'framework. Pass `remove` with the requirement ids to detach.');
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
const timings = {};
|
|
643
|
+
const subdomainRead = (0, shared_js_1.readSubdomain)(client, timings);
|
|
644
|
+
const adding = [...new Set(requirementIds?.add ?? requirementIds?.set ?? [])];
|
|
645
|
+
if (adding.length > 0) {
|
|
646
|
+
const unknown = await requirementsToAddRefusal(client, adding, startedAt + shared_js_1.AUTHORING_READ_DEADLINE_MS, timings);
|
|
647
|
+
if (unknown)
|
|
648
|
+
return unknown;
|
|
649
|
+
}
|
|
650
|
+
let updated;
|
|
651
|
+
try {
|
|
652
|
+
updated = await (0, shared_js_1.timed)(timings, 'update', () => client.updateControl({ id: controlId, ...Object.fromEntries(changes), requirementIds }));
|
|
653
|
+
}
|
|
654
|
+
catch (error) {
|
|
655
|
+
if ((0, shared_js_1.isNotFound)(error)) {
|
|
656
|
+
return (0, result_js_1.structuredResult)({
|
|
657
|
+
outcome: 'not_found',
|
|
658
|
+
controlId,
|
|
659
|
+
note: 'No non-deleted control has this id, and nothing changed.',
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
if ((0, shared_js_1.isAborted)(error)) {
|
|
663
|
+
return (0, shared_js_1.interruptedWrite)('the control was being updated', 'Read the control with `get-control` before repeating the update.');
|
|
664
|
+
}
|
|
665
|
+
throw error;
|
|
666
|
+
}
|
|
667
|
+
const subdomain = await (0, shared_js_1.subdomainBy)(subdomainRead, startedAt + shared_js_1.AUTHORING_READ_DEADLINE_MS);
|
|
668
|
+
return (0, result_js_1.structuredResult)({
|
|
669
|
+
outcome: 'updated',
|
|
670
|
+
control: projectWritten(updated, subdomain),
|
|
671
|
+
note: 'Saved. `get-control` can lag this write by a few seconds; a repeat with the same input changes nothing.',
|
|
672
|
+
timingsMs: timings,
|
|
673
|
+
});
|
|
674
|
+
},
|
|
675
|
+
}),
|
|
676
|
+
(0, types_js_1.defineTool)({
|
|
677
|
+
name: 'delete-control',
|
|
678
|
+
title: 'Delete Draft Control',
|
|
679
|
+
description: (0, load_description_js_1.loadDescription)('delete-control.md'),
|
|
680
|
+
inputSchema: {
|
|
681
|
+
controlId: controlIdInput().describe('The exact control id of a DRAFT that was never LIVE.'),
|
|
682
|
+
},
|
|
683
|
+
outputSchema: schemas_js_1.deleteControlOutput,
|
|
684
|
+
// A repeat sees not_found, which is what the first delete left.
|
|
685
|
+
annotations: { ...types_js_1.MUTATE, idempotentHint: true },
|
|
686
|
+
accessDeniedHint: shared_js_1.ACCESS_DENIED_HINT,
|
|
687
|
+
errorContext: 'Error deleting control',
|
|
688
|
+
handler: async ({ controlId }, client) => {
|
|
689
|
+
const startedAt = Date.now();
|
|
690
|
+
const readDeadline = startedAt + shared_js_1.AUTHORING_READ_DEADLINE_MS;
|
|
691
|
+
const timings = {};
|
|
692
|
+
const subdomainRead = (0, shared_js_1.readSubdomain)(client, timings);
|
|
693
|
+
// Revoked ones too: an attestation is filed only on a LIVE control, so any attestation,
|
|
694
|
+
// revoked or not, is history this hard delete would purge (PR #59 review, 2026-10-30).
|
|
695
|
+
const attestationsRead = client
|
|
696
|
+
.listAttestations({ controlId, includeDeleted: true })
|
|
697
|
+
.catch((error) => {
|
|
698
|
+
console.error("Error reading a control's attestations before a delete:", error);
|
|
699
|
+
return 'unreadable';
|
|
700
|
+
});
|
|
701
|
+
const requirementsRead = client.listControlRequirements(controlId).catch((error) => {
|
|
702
|
+
console.error("Error reading a control's requirements before a delete:", error);
|
|
703
|
+
return 'unreadable';
|
|
704
|
+
});
|
|
705
|
+
const attestations = await (0, shared_js_1.boundedRead)(timings, 'attestations', () => attestationsRead, readDeadline);
|
|
706
|
+
// The control is read last, nearest the delete: the backend checks only permissions, so the
|
|
707
|
+
// state this gate decides on should be as fresh as it can be.
|
|
708
|
+
const control = await (0, shared_js_1.boundedRead)(timings, 'control', () => (0, shared_js_1.findControl)(client, controlId), readDeadline);
|
|
709
|
+
if (control === shared_js_1.DEADLINE_PASSED)
|
|
710
|
+
return (0, shared_js_1.readFailed)('The control', { controlId });
|
|
711
|
+
if (!control) {
|
|
712
|
+
return (0, result_js_1.structuredResult)({
|
|
713
|
+
outcome: 'not_found',
|
|
714
|
+
controlId,
|
|
715
|
+
note: 'No control has this id, which is also what a repeated delete sees; nothing changed.',
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
const refuse = (reason, detail) => (0, shared_js_1.refusal)('not_deletable', `Control not deleted — ${detail}`, {
|
|
719
|
+
controlId,
|
|
720
|
+
reason,
|
|
721
|
+
control: { id: control.id, name: control.name, state: control.state },
|
|
722
|
+
nextStep: keepInstead(control.state),
|
|
723
|
+
});
|
|
724
|
+
if (control.state !== 'DRAFT')
|
|
725
|
+
return refuse('state', `it is ${control.state ?? 'in no state'}, not DRAFT. ${keepInstead(control.state)}`);
|
|
726
|
+
if (control.lastEvaluatedOn)
|
|
727
|
+
return refuse('was_live', 'it has been scored as a LIVE control, so its history is kept. ' +
|
|
728
|
+
keepInstead(control.state));
|
|
729
|
+
if (attestations === 'unreadable' || attestations === shared_js_1.DEADLINE_PASSED)
|
|
730
|
+
return (0, shared_js_1.readFailed)("The control's attestations", { controlId });
|
|
731
|
+
if (attestations.length > 0) {
|
|
732
|
+
return refuse('has_attestations', `it carries ${attestations.length} attestation${attestations.length === 1 ? '' : 's'} ` +
|
|
733
|
+
'(revoked ones included), so it has been LIVE and its history is kept. ' +
|
|
734
|
+
keepInstead(control.state));
|
|
735
|
+
}
|
|
736
|
+
try {
|
|
737
|
+
await (0, shared_js_1.timed)(timings, 'delete', () => client.deleteControl(controlId));
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
if ((0, shared_js_1.isNotFound)(error)) {
|
|
741
|
+
return (0, result_js_1.structuredResult)({
|
|
742
|
+
outcome: 'not_found',
|
|
743
|
+
controlId,
|
|
744
|
+
note: 'The control was already gone; nothing changed.',
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
if ((0, shared_js_1.isAborted)(error)) {
|
|
748
|
+
return (0, shared_js_1.interruptedWrite)('the control was being deleted', 'Call `delete-control` again: `not_found` confirms it.');
|
|
749
|
+
}
|
|
750
|
+
throw error;
|
|
751
|
+
}
|
|
752
|
+
const requirements = await (0, shared_js_1.withDeadline)(requirementsRead, readDeadline);
|
|
753
|
+
const tests = (control.controlTests ?? []).filter((test) => !test.deleted);
|
|
754
|
+
await (0, shared_js_1.subdomainBy)(subdomainRead, readDeadline);
|
|
755
|
+
return (0, result_js_1.structuredResult)({
|
|
756
|
+
outcome: 'deleted',
|
|
757
|
+
control: { id: control.id, name: control.name, state: control.state },
|
|
758
|
+
testsRemoved: tests.length,
|
|
759
|
+
requirements: requirements === shared_js_1.DEADLINE_PASSED || requirements === 'unreadable'
|
|
760
|
+
? []
|
|
761
|
+
: requirements.requirements.map((entry) => ({
|
|
762
|
+
id: entry.id,
|
|
763
|
+
title: entry.title,
|
|
764
|
+
identifier: entry.identifier,
|
|
765
|
+
})),
|
|
766
|
+
note: 'Deleted, with its tests. A replacement goes under the same requirements with ' +
|
|
767
|
+
'`create-controls`.',
|
|
768
|
+
timingsMs: timings,
|
|
769
|
+
});
|
|
770
|
+
},
|
|
771
|
+
}),
|
|
772
|
+
];
|
|
773
|
+
//# sourceMappingURL=authoring.js.map
|